Unity c#脚本引用返回null

时间:2016-07-07 12:36:05

标签: c# unity5

我正在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(显然)。播放器是一个空白的板块(此时),ClientController只是与网络结合使用的基本内容。

我收到此错误:

  

[NullReferenceException:对象引用未设置为对象的实例]

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

我觉得好像我犯了一个简单而又根本性的错误,但我不能为我的生活包围我的想法。

我尝试从播放器中删除monobehaviour继承并使用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!

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

1 个答案:

答案 0 :(得分:0)

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