简体   繁体   English

Unity-实例化新对象时为空引用

[英]Unity - Null Reference when instantiating new object

I'm trying to cycle through a 2D array of card objects and instantiate them in my Unity scene. 我试图遍历卡片对象的2D数组,并在Unity场景中实例化它们。 However, I'm getting a Null reference exception when trying to instantiate them. 但是,尝试实例化它们时,我得到了Null引用异常。 Here is the code that instantiates the objects: 这是实例化对象的代码:

//Setting up initial board
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 4; j++){
            //Checked with debug.log, this should work Debug.Log (board[j, i].name);
            Debug.Log(board[j, i].name);
            temp = Instantiate(board[j,i]) as GameObject;
            CardScript cs = temp.GetComponent<CardScript>();
            objectBoard[j, i] = cs;
            //Setting locations of the cards
            objectBoard[j, i].transform.position = new Vector3(30 * j + 20, 50 * i +   70, 0);
        }
    }

The error occurs in the line 'CardScript cs = new.... I originally had the error in the temp = Instantiate... line, when the code was GameObject temp = Instantiate.... It fixed when I made temp a private GameObject variable in the code. 该错误发生在“ CardScript cs = new ....”行中,当代码为GameObject temp = Instantiate ....时,我最初在temp = Instantiate ...行中发生了错误。代码中的GameObject变量。 I don't think I can do that with this one, because I need to have a reference to each individual object I'm instantiating. 我认为我不能用这个来做到这一点,因为我需要引用我要实例化的每个对象。

Full Code: 完整代码:

public class MatchScript : MonoBehaviour {

public CardScript[] potentialCards;

private CardScript[,] board;
private CardScript[,] objectBoard;
private List<CardScript> entries;
private GameObject temp;


// Use this for initialization
void Start () {
    entries = new List<CardScript> ();
    objectBoard = new CardScript[4,3];
    board = new CardScript[4, 3];
    foreach (CardScript c in potentialCards)
        entries.Add (c);
    foreach (CardScript c in potentialCards)
        entries.Add (c);

    //Loading up the board
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 4; j++){
            int k = Random.Range(0, entries.Count);
            board[j, i] = entries[k];

            entries.RemoveAt(k);
        }
    }

    //Setting up initial board
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 4; j++){
            //Checked with debug.log, this should work Debug.Log (board[j, i].name);
            Debug.Log(board[j, i].name);
            temp = Instantiate(board[j,i]) as GameObject;
            CardScript cs = temp.GetComponent<CardScript>();
            objectBoard[j, i] = cs;
            //Setting locations of the cards
            objectBoard[j, i].transform.position = new Vector3(30 * j + 20, 50 * i + 70, 0);
        }
    }
}

// Update is called once per frame
void Update () {

}

I'm not really sure why this happen. 我不太确定为什么会这样。 But you can fix this by change 但是你可以通过改变来解决

temp = Instantiate(board[j,i]) as GameObject;

to

temp = Instantiate(board[j,i].gameObject) as GameObject;

"expression as Type" clause returns expression cast to Type if and only if actual type of expression is derived from Type, otherwise it returns null. 当且仅当表达式的实际类型是从Type派生时,“ expression as Type”子句才将表达式强制转换为Type,否则返回null。

Instantiate makes an independent copy of an object passed, which, naturally, has type CardScript. 实例化对传递的对象进行独立复制,该对象自然具有CardScript类型。 Now, there is some possible misunderstanding: GameObject is a base class for all "entities in Unity scenes", but that doesn't include all classes you create on your own. 现在,可能会有一些误解:GameObject是所有“ Unity场景中的实体”的基类,但其中不包括您自己创建的所有类。 If CardScript is a class you created and it doesn't inherit any class explicitly, then it inherits System.Object (which is base class for all C# classes). 如果CardScript是您创建的类,并且没有显式继承任何类,则它将继承System.Object(这是所有 C#类的基类)。

If CardScript is derived from GameObject, then you should probably provide that class as well. 如果CardScript是从GameObject派生的,那么您可能还应该提供该类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM