C# 返回实例化对象时是否为空对象?
从GameController创建新的Tetromino时,我返回一个Gameobject,但它返回的是一个空对象。产卵器代码中的日志显示的是正确的对象,但gamecontroller中的日志仅显示对象 产卵器代码:C# 返回实例化对象时是否为空对象?,c#,unity3d,gameobject,C#,Unity3d,Gameobject,从GameController创建新的Tetromino时,我返回一个Gameobject,但它返回的是一个空对象。产卵器代码中的日志显示的是正确的对象,但gamecontroller中的日志仅显示对象 产卵器代码: public class Spawner : MonoBehaviour { public GameObject[] Tetrominoes; private List<GameObject> instantiatedTetrominioes = new
public class Spawner : MonoBehaviour
{
public GameObject[] Tetrominoes;
private List<GameObject> instantiatedTetrominioes = new List<GameObject>();
private int index = 0;
// Start is called before the first frame update
void Start()
{
NewTetromino();
}
public GameObject NewTetromino()
{
GameObject newObject = Instantiate(Tetrominoes[Random.Range(0, Tetrominoes.Length)], transform.position, Quaternion.identity) as GameObject;
instantiatedTetrominioes.Add(newObject);
index++;
Debug.Log(instantiatedTetrominioes[index - 1]);
return instantiatedTetrominioes[index - 1];
}
}
您还可以在
Spawner
的void Start
中调用newetromino
。因此,您可能会看到Spawner.Start
->newteromino
调用的日志,然后当您在ongamestart
上调用Spawner.newteromino
时,您会从newteromino
获得一个空的调试日志,并且从ongamestart
获得一个“空”调试日志(除了它包含文本“当前”).这可能就是问题所在,谢谢!
void OnGameStarted()
{
currentBlock = spawner.NewTetromino();
Debug.Log("current: " + currentBlock);
}