2014-12-06 427 views
2

出於某種原因,我得到它看起來像這樣的錯誤:Unity2D:UnassignedReferenceException:變量尚未分配實例化

UnassignedReferenceException: The variable LevelComplete of NPad1 has not been assigned. 

,這是我的代碼:

public class NPad1 : MonoBehaviour { 
public Sprite img1 , img2; 
public Rigidbody2D LevelComplete; 

void Start() { 
    gameObject.GetComponent<SpriteRenderer>().sprite = img1; 
} 

// Update is called once per frame 
void OnTriggerEnter2D(Collider2D other) { 
    gameObject.GetComponent<SpriteRenderer>().sprite = img2; 
    Instantiate (LevelComplete); 
} 

我做了什麼錯?

回答

1

參照this;

克隆對象原始並返回克隆。

這意味着你將不得不調用Instantiate(),它返回現有對象的克隆之前實例LevelComplete。您可能沒有在Unity檢查器中設置實例。欲瞭解更多信息,請訪問this guide關於設置公共變量。

+0

是的,你是對的,我把這段代碼放在我的對象上而不是我的預製件上,非常感謝! – 2014-12-06 19:28:24