【问题标题】:GameObject won't change after updating it with a Method (object reference not set)使用方法更新游戏对象后不会更改(未设置对象引用)
【发布时间】:2020-05-19 20:13:21
【问题描述】:

Unity 新手在这里 :)

我有一个按钮,它调用这个函数,它基本上设置了一个要实例化的预制件。

  public void setTurret()
{
    towerNode.setTurretType(turret)
    Debug.Log("selected shop turret:" + turret.name);
}

我的Tower_Node 类处理实际的实例化

 public class Tower_Node : MonoBehaviour
    {
        private GameObject turret;

               public void setTurretType(TowerType _turret) 
                   {
                        turret= _turret.prefab;
                    }



               private void OnMouseDown()
               {             
                Instantiate(turret,GetBuildPosition(),Quaternion.identity);
               }
    ...}

编辑:我也试过了

      public class Tower_Node : MonoBehaviour
        {
            private TowerType turret;

                   public void setTurretType(TowerType _turret) 
                       {
                            turret = _turret;
                    }



               private void OnMouseDown()
               {             
                Instantiate(turret.prefab,GetBuildPosition(),Quaternion.identity);
               }
    ...}

编辑: This is how the references in the inspector look. The shop script is the script with the setTurret() method

--

使用setTurretType 方法设置炮塔有效。如果我用 Debug.Log() 检查它,我得到了正确的 TowerType,但在函数之外,游戏对象仍然是 =null,当我尝试实例化它时,它会给我一个 NullReferenceException(因为 Gameobject 显然是 null)

我在这里错过了什么?

感谢您的回答。

【问题讨论】:

  • 是否有可能有多个 Tower_Node 实例,但您没有在所有实例上都使用 setTurretType
  • 我的Tower_Node 是一个预制件,它在我的场景中多次存在。 setTurretType 应该更新所有这些,因为它是预制的,对吧?

标签: c# unity3d gameobject


【解决方案1】:

切换 OnMouseDown() 为

if (Input.GetMouseButtonDown(0))

在你的更新循环中

只要确保 turret.prefab 是一个预制件

Instantiate(turret.prefab, GetBuildPosition(),Quaternion.identity);

【讨论】:

  • 这不起作用,是的,我确定预制件是预制件!
【解决方案2】:

我想说,如果你的 TowerType 类有一个名为 prefab 的 GameObject var,你应该将 prefab 分配给 prefab,或者将炮塔分配给 _turret 并访问 prefab。

要么:

public void setTurretType(TowerType _turret) {
    turret = _turret;
    hoverTurret = _turret.hoverPrefab;  
}

private void OnMouseDown()
{
    Instantiate(turret.prefab, GetBuildPosition(),Quaternion.identity);
}

public void setTurretType(TowerType _turret) {
    turret.prefab = _turret.prefab;
    hoverTurret = _turret.hoverPrefab;  
}

private void OnMouseDown()
{
    Instantiate(turret.prefab, GetBuildPosition(),Quaternion.identity);
}

应该可以。

编辑: 您可以在实例化时设置炮塔:

public void setTurretType(TowerType _turret) {
    turret.prefab = _turret.prefab;
    hoverTurret = _turret.hoverPrefab;  
}

private void OnMouseDown()
{
    turret = Instantiate(turret.prefab, GetBuildPosition(),Quaternion.identity);
}

【讨论】:

  • 是的,我的 TowerType 类有一个 public GameObject prefab。我都试过了,但不幸的是这不起作用,对象仍然不会更新
  • 您是否在代码中的某处将预制件设置为空?
  • 不,我不是唯一修改它的代码
  • 遗憾的是这也不起作用,即使我将 var 设置为 [SerializeField] 这也不起作用,但我可以在检查器中看到实际预制件是如何交换的。我真的不明白为什么它为空
  • 那么我想说你需要提供更多关于你的其他课程的信息......
猜你喜欢
  • 2022-01-20
  • 2019-10-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-11
  • 1970-01-01
相关资源
最近更新 更多