为什么我在这种特定情况下得到NullReferenceException?

时间:2015-07-22 08:38:56

标签: c# unity3d nullreferenceexception

这是我在启动游戏时看到的内容:http://i.imgur.com/lwfkWlw.png当我按下“空格”时,会发生最后一个错误。

好的,所以我甚至从编辑器手动将“ResourcesScript”放到“Cube”上,但仍然无法通过“GetComponenet();”找到它。 :S Actor类最初用于创建组件(并且它的ITS引用存在且不是null):

void Awake () {
    resource = gameObject.AddComponent <ResourcesScript> ();
}

这是整个ResourcesScript:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class ResourcesScript : MonoBehaviour {
    public GameObject attatchedGameObject;
    public Rigidbody attatchedRigidbody;

    public BehaviourStateResource behaviourResource;
    public BehaviourStateManager behaviourManager;

    public MovementStats moveStats;
    public MovementStatsManager moveManager;
    public State currentMovementState;

    public HealthStats healthStats;
    public HealthStats armorStats;
    public HealthStats shieldStats;
    //public HealthManager healthManager;

    void Start () {
        attatchedGameObject = gameObject;
        attatchedRigidbody = gameObject.GetComponent <Rigidbody> ();

        behaviourResource = new BehaviourStateResource ();
        behaviourManager = gameObject.AddComponent <BehaviourStateManager> ();

        moveStats = new MovementStats (new Stat (10.0f), new Stat (10.0f), 10.0f, new Stat (10.0f), new Stat (10.0f), 10.0f);
        moveManager = new MovementStatsManager (this);
        currentMovementState = gameObject.AddComponent <DefaultState> ();

        healthStats = new HealthStats (new Stat (10.0f), new Stat (10.0f), new Stat (10.0f));
        armorStats = new HealthStats (new Stat (10.0f), new Stat (10.0f), new Stat (10.0f));
        shieldStats = new HealthStats (new Stat (10.0f), new Stat (10.0f), new Stat (10.0f));
        //healthManager = gameObject.AddComponent <HealthManager> ();
    }
}

最后,这是一个使用GetComponent获取对象的类的示例...但未能找到一个:

public class BehaviourStateManager : MonoBehaviour {
    public ResourcesScript resource;

    void Start () {
        resource = gameObject.GetComponent <ResourcesScript> ();
        Utilities.Validate <ResourcesScript> (resource, this);
    }
}

我已经这么久了,我似乎无法找到问题。

0 个答案:

没有答案