所以,我在 Unity 中遇到了這個問題,我在 GameManager 中創建了一個玩家,然后我將它實體化到 BoardManager 并想在 GameManager 中使用回合系統,玩家腳本在另一個名為 PlayerScript 的檔案中。我想在 BoardManager 中創建玩家,然后在 GameManager 中使用它。當我嘗試呼叫函式 move() 時,出現錯誤“協程無法啟動,因為游戲物件‘玩家’處于非活動狀態”。
Obs1:我將玩家分配給 instance2,以便我可以在 GameManager 中使用它。
Obs2:問題在于協程沒有初始化,所有運動都在作業,呼叫方法也是如此,在我看來,播放器不存在或其他原因無法呼叫該函式,我以不同的方式測驗了代碼,但似乎什么都沒有去作業。
在此先感謝您的幫助 :)
下面的代碼來自 BoardManager:
//map = grid of 50 per 50 of numbers, these numbers says if a floor, wall, player,
//enemies, etc are going to be placed in the grid done from procedural generation
for (int i = 0; i < rows; i )
{
for (int j = 0; j < columns; j )
{
if (map[i,j] == 0)
{
GameObject instance = Instantiate(wallTile, new Vector3(i, j, 0f), Quaternion.identity) as GameObject;
}
if (map[i,j] == 1)
{
GameObject instance = Instantiate(floorTile, new Vector3(i, j, 0f), Quaternion.identity) as GameObject;
}
if (map[i,j] == 2)
{
GameObject instance1 = Instantiate(floorTile, new Vector3(i, j, 0f), Quaternion.identity) as GameObject;
GameObject instance2 = Instantiate(player, new Vector3(i, j, 0f), Quaternion.identity) as GameObject;
player = instance2;
}
}
}
下一段代碼來自 GameManager:基本上,我嘗試從地圖中的玩家那里獲取腳本,并使用該腳本中的移動功能。
public class GameManager : MonoBehaviour
{
public static BoardManager boardScript;
private int PLAYER_TURN = 1;
private int ENEMY_TURN = 2;
private int game_state;
public GameObject player;
void Awake()
{
int[,] map = new int[0,0];
boardScript = GetComponent<BoardManager>();
boardScript.makeMap(map, player);
}
public void Update()
{
game_state = PLAYER_TURN;
if(Input.anyKey)
{
char c = Input.inputString[0];
player.GetComponent<PlayerScript>().movement(c);
game_state = ENEMY_TURN;
}
}
}
}
下一段代碼來自 PlayerScript
public class PlayerScript : MonoBehaviour
{
private bool isMoving;
private Vector3 origPos, targetPos;
private float timeToMove = 0.2f;
public void movement(char c)
{
if (c == 'w' && !isMoving)
StartCoroutine(movePlayer(Vector3.up));
if (c == 'a' && !isMoving)
StartCoroutine(movePlayer(Vector3.left));
if (c == 's' && !isMoving)
StartCoroutine(movePlayer(Vector3.down));
if (c == 'd' && !isMoving)
StartCoroutine(movePlayer(Vector3.right));
if (c == 'q' && !isMoving)
StartCoroutine(movePlayer(new Vector3(-1, 1, 0)));
if (c == 'e' && !isMoving)
StartCoroutine(movePlayer(new Vector3(1, 1, 0)));
if (c == 'c' && !isMoving)
StartCoroutine(movePlayer(new Vector3(1, -1, 0)));
if (c == 'z' && !isMoving)
StartCoroutine(movePlayer(new Vector3(-1, -1, 0)));
}
private IEnumerator movePlayer(Vector3 direction)
{
isMoving = true;
float elapsedTime = 0;
origPos = transform.position;
targetPos = origPos direction;
while(elapsedTime < timeToMove)
{
transform.position = Vector3.MoveTowards(origPos, targetPos, (elapsedTime / timeToMove));
elapsedTime = Time.deltaTime;
yield return null;
}
transform.position = targetPos;
isMoving = false;
}
}
uj5u.com熱心網友回復:
問題在于您如何將player變數傳遞給boardscript.MakeMap()函式。由于函式引數,除非指定,否則按值傳遞而不是按參考傳遞。這意味著您實際上是在為要使用的函式制作變數的副本。該函式可以為其區域作用域分配該變數的值,但它不會影響在函式外部傳入的變數。請參閱此處:https : //docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/passing-parameters
要解決此問題,您可以使用void makeMap(int[][] map, ref GameObject player),然后呼叫它,您需要這樣做boardScript.makeMap(map, ref player);
參考檔案:https : //docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/ref
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/388818.html
上一篇:如何使用CodeIgniter3中的外鍵從表中獲取列資料
下一篇:將相機回傳給游戲物件的C#腳本
