c# - 返回 null 的 Unity c# 脚本引用

标签 c# unity3d

我正在 Unity 中制作快速的 2 人游戏,我想到了这个:

using UnityEngine;
using System.Collections;

    public class PlayerClient : MonoBehaviour {

    public string username;
    public RaceType currentRace = RaceType.Human ;
    GameObject playerMob;
    Player currentPlayer;
    ClientController attachedController;
    // Use this for initialization
    void Start () {
        attachedController = gameObject.GetComponent (typeof(ClientController)) as ClientController;
        if(attachedController == null)
            Debug.LogError("Could not create attachedController!");
    }

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

    }

    public void MovementInput(float x, float y, float z){
        playerMob.transform.Translate (x, y, z);
    }

    public void FinishCharacterCreation(){
        switch (currentRace) {
        default:
            break;
        case RaceType.Human:
            playerMob = Instantiate (Resources.Load ("Human", typeof(GameObject))) as GameObject;
            currentPlayer = playerMob.AddComponent<Player> () as Player;
            attachedController.enabled = true;
            break;
        }
    }
}

里面有3个类; PlayerClientPlayerClientController

显示的是 PlayerClient(很明显)。 Player 是一张白纸(此时),而 ClientController 只是与网络结合使用的基本东西。

我收到这个错误:

[NullReferenceException: Object reference not set to an instance of an object]

在第 24 和 34 行,正如您将看到的,这是引用 playerMob(只是一个空白的 3d 立方体)和 attachedController 的行。据我所知,它们似乎并没有保存到定义的变量中,但是在第 32 和 33 行中没有错误,所以它似乎只是保存到一个临时的、只有函数的变量而不是类变量(“人类”预制件制作成功)。

我觉得我好像犯了一个简单但根本性的错误,但我无法终生思考它可能是什么。

我已经尝试从播放器中删除单一行为继承并使用 new() 关键字而不是实例化它,但同样的事情发生了。我做错了什么?

编辑:这是我做的一个小测试。

using UnityEngine;
using System.Collections;

public class Client : MonoBehaviour {

string username;
Player player;
GameObject playerMob;
ClientController clientController;

// Use this for initialization
void Start () {
    clientController = gameObject.GetComponent( typeof(ClientController) ) as ClientController;
    if(clientController == null){
        Debug.LogWarning("Client Controller is null!");
    } else {
        Debug.LogWarning ("Client Controller is NOT null!");
    }
}

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

}

public void FinishCharacterCreation(){
    player = new Player ();
    if (player == null) {
        Debug.LogWarning ("Player is null!");
    } else {
        Debug.LogWarning ("Player is NOT null!");
        playerMob = Instantiate (Resources.Load ("Human", typeof(GameObject))) as GameObject;
    }
    if (playerMob == null) {
        Debug.LogWarning ("PlayerMob is null!");
    } else {
        Debug.LogWarning ("PlayerMob is NOT null!");
    }
    if (clientController == null) {
        Debug.LogWarning ("ClientController is null!");
    } else {
        Debug.LogWarning ("ClientController is NOT null!");
    }

}

到处都是调试日志警告。播放时我得到的是:

Client Controller is NOT null!
Player is NOT null!
PlayerMob is NOT null!
ClientController is null!

注意最后的日志。为什么会这样? FinishCharacterCreation() 显然是在设置 clientController 变量后调用的。除了您在那里看到的脚本和对象之外,所有脚本和对象都是完全空的。 (Player 不是 monobehaviour 固有的,ClientController 是)FinishCharacterCreation() 由 UI 按钮调用,Client 附加的 GameObject 是由基本 NetworkManager 对象制成的预制件。

最佳答案

什么时候调用 FinishCharacterCreation?可能会在 Start 之前调用 FinishCharacterCreation,这会导致 attachedController 为 null。实例化 gameObject 时,调用 Start 可能需要一两帧,因此您可能过早地调用了 FinishCharacterCreation。我会放入一些 Debug.Logs 并确保所有函数都按照您期望的顺序被调用。

关于c# - 返回 null 的 Unity c# 脚本引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38245636/

相关文章:

c# - 绘制函数中的曼德布罗分形误差

c# 类型别名/自定义类型

serialization - GameObject 序列化时的回调

c# - 结构扩展方法

c# - 正则表达式匹配方括号内括号内的数字和可选文本

c# - 当应用程序被停用并通过应用程序列表重新启动时,我应该启动新实例还是最后恢复?

c# - DataGridView多行选择鼠标左键拖放清除

unity3d - 如何从命令行设置统一构建场景路径

c# - 在 Unity3D 中使用第一人称 Controller 在空中移动,c#

c# - 在自定义检查器中分配引用时如何调用函数?