Unity C# NullReferenceException:对象引用未设置为对象的实例

Posted

技术标签:

【中文标题】Unity C# NullReferenceException:对象引用未设置为对象的实例【英文标题】:Unity C# NullReferenceException: Object reference not set to an instance of an object 【发布时间】:2014-07-21 12:19:50 【问题描述】:
using UnityEngine;

使用 System.Collections;

公共类 GameHandler : MonoBehaviour

public GameObject playerPrefab;
public GameObject playerCameraPrefab;
public GameObject player;
public GameObject playerCamera;
// Use this for initialization
void Awake () 
    player = Instantiate(playerPrefab,new Vector3(5,1,5),Quaternion.identity) as GameObject;
    playerCamera = Instantiate(playerCameraPrefab,Vector3.up,Quaternion.identity) as GameObject;


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


using UnityEngine;

使用 System.Collections; 使用 System.Collections.Generic;

公共类 EarthMagic : MonoBehaviour

public GameHandler gameHandler;

public GameObject EarthMagicWall;
public PlayerMovement PM;
public UIControls UI;
public Transform spinner;

public List<Transform> circleStones = new List<Transform>();

// Use this for initialization
void Start () 
    //connect to the gameHandler
    gameHandler = GameObject.Find ("WorldHandler").GetComponent("GameHandler") as GameHandler;
    PM = transform.GetComponent("PlayerMovement") as PlayerMovement;
    UI = transform.GetComponent("UIControls") as UIControls;


// Update is called once per frame
void Update () 
    if(UI.gamePaused == false) 
        if(Input.GetButtonDown("Channel")) 
            if(PM.isGrounded == true) 
                Vector3 checkPosition = transform.position + (transform.forward * 3);

            //  Debug.DrawRay(transform.position, checkPosition, Color.blue, 20);
            //  Debug.DrawRay(checkPosition, transform.TransformDirection (Vector3.down), Color.red, 20);

                RaycastHit hit;
                if (Physics.Raycast(checkPosition, transform.TransformDirection (Vector3.down), out hit, 5)) 
                    if(hit.collider.name == "Terrain") 
                        Instantiate(EarthMagicWall, hit.point, transform.rotation);
                    
                
            
        

        if(Input.GetMouseButtonDown(0)) 
            if(circleStones.Count > 0) 
                Transform temp = circleStones[0].transform;
                circleStones.RemoveAt(0);
                temp.parent = null;
                temp.rigidbody.AddForce(Camera.main.transform.up * 20);
            
        
        ControlRocks ();
    


void ControlRocks() 
    foreach(Transform circleStone in circleStones) 
        switch(circleStones.Count) 
        case 1: case 2:
            if(circleStones.IndexOf(circleStone) == 1) 
                circleStone.transform.localPosition = new Vector3(0,0, 2); 
             else 
                circleStone.transform.localPosition = new Vector3(0,0, -2);
            
            break;
        case 3:
            if(circleStones.IndexOf(circleStone) == 1) 
                circleStone.transform.localPosition = new Vector3(0,0, 2); 
             else if(circleStones.IndexOf(circleStone) == 2) 
                circleStone.transform.localPosition = new Vector3(Mathf.Sqrt(2),0, (Mathf.Sqrt(2) * -1));
             else 
                circleStone.transform.localPosition = new Vector3((Mathf.Sqrt(2) * -1),0, (Mathf.Sqrt(2) * -1));
            
            break;
        case 4:
            if(circleStones.IndexOf(circleStone) == 1) 
                circleStone.transform.localPosition = new Vector3(Mathf.Sqrt(2),0, Mathf.Sqrt(2));  
             else if(circleStones.IndexOf(circleStone) == 2) 
                circleStone.transform.localPosition = new Vector3(Mathf.Sqrt(2),0, (Mathf.Sqrt(2) * -1));
             else if(circleStones.IndexOf(circleStone) == 3)
                circleStone.transform.localPosition = new Vector3((Mathf.Sqrt(2) * -1),0, (Mathf.Sqrt(2) * -1));
             else 
                circleStone.transform.localPosition = new Vector3((Mathf.Sqrt(2) * -1),0, Mathf.Sqrt(2));
            
            break;
        case 5:
            if(circleStones.IndexOf(circleStone) == 1) 
                circleStone.transform.localPosition = new Vector3(0,0, 2); 
             else if(circleStones.IndexOf(circleStone) == 2) 
                circleStone.transform.localPosition = new Vector3(1.9f,0, 0.62f);
             else if(circleStones.IndexOf(circleStone) == 3)
                circleStone.transform.localPosition = new Vector3(1.18f,0, -1.61f);
             else if(circleStones.IndexOf(circleStone) == 4)
                circleStone.transform.localPosition = new Vector3(-1.18f,0, -1.61f);
             else 
                circleStone.transform.localPosition = new Vector3(-1.9f,0, 0.62f);
            
            break;
        

        Stone stone = circleStone.gameObject.GetComponent("Stone") as Stone;
        if(!stone.isRigid) 
            Rigidbody shardRigidBody = stone.gameObject.AddComponent<Rigidbody>();
            shardRigidBody.mass = 5;
            stone.isRigid = true;
        
    

所以上面的错误发生在 EarthMagic 的第 63 行。我以前解决过这种类型的错误,但这次我真的不明白。这段代码以前可以工作,我没有更改整个函数。然而,我确实开始实例化我的播放器和相机,而不是简单地将它们放在编辑器中。我确定这是问题的原因,但我真的不知道如何解决这个问题。有什么想法吗?

【问题讨论】:

第 63 行是哪一个? ;) 您有公共变量,需要将 GameObjects 拖到它们的位置,以便它们知道它们所引用的内容...确保您已修复这些框并返回您的结果...如果您已经已经完成了,那么我们需要您提供更多信息 是的,我已经完成了 Savlon。你需要了解什么?第 63 行是 circleStone.transform.localPosition = new Vector3(0,0, -2); What is a NullReferenceException and how do I fix it?的可能重复 【参考方案1】:

您的列表中有一个空元素。代码很多,你应该自己调试,它会对你有所帮助。

现在你可以实现这个简单的 Hack。

foreach(Transform circleStone in circleStones) 

    if(circleStone == null)
       continue;       

    switch(circleStones.Count) 
    
     . 
     .
     .
    
 

此代码将检查是否为 null,如果找到则将跳过该元素。

【讨论】:

很奇怪。这解决了大部分问题,我设法自己解决了其余问题,但为什么我以前不需要呢?我试图调试它但卡住了。所以我来这里寻求帮助。不管怎样,谢谢。这真的让我很困扰。 你在上面的代码中做了一些使一个或多个元素为空的事情。祝你游戏好运!

以上是关于Unity C# NullReferenceException:对象引用未设置为对象的实例的主要内容,如果未能解决你的问题,请参考以下文章

在unity3D: c# 怎样调用另外一个c#脚本里面东西?

[Unity] C#中级编程 - 08 - 接口

[Unity] C#中级编程 - 03 - 重载

Unity游戏开发C#基础认识与了解

Unity3DUnity 脚本 ② ( Visual Studio 2019 中的 Unity 编译环境配置 | Unity 编辑器关联外部 C# 脚本编辑器 Visual Studio )

Unity修改C#范文脚本