Unity NullReferenceException错误

时间:2014-03-25 21:32:18

标签: c# windows reference unity3d nullreferenceexception

我试图从魔杖射出一盏灯,而且当我运行游戏时,我一直收到这个错误。灯仍亮,但错误出现在控制台中。我创建了一个轻型预制件,然后将其拖到我的脚本" Light_Spell"上。这是代码:

using UnityEngine;
using System.Collections;

public class Light_Spell : MonoBehaviour {

    //public Light currentSpellLight;
    private ChangeSpell changespell;
    private int selectedSpellNum;
    private bool isSelectedSpell;

    private Light wandLight;
    private bool triggerIsPressed;

    private float life = 1.0f;
    private float speed = 15.0f;
    private float timeToDie = 1.0f;

    [SerializeField]
    public Light lightProjectile;

    // Find the light object and disable it
    void Start () {

        isSelectedSpell = true;

        wandLight = GameObject.Find ("Spell_Light").light;

        wandLight.enabled = false;
        triggerIsPressed = false;
    }

    // Handles button presses etc
    void Update () {
        //changespell = currentSpellLight.GetComponent<changespell>();

        if(changespell == null)
            Debug.Log("error");
        //Gets the currently selected spell number
        selectedSpellNum = changespell.GetSelectedSpellNumber();

        //Sets whether it is the selected spell  
        if(selectedSpellNum == 3)
            isSelectedSpell = true;
        else
            isSelectedSpell = false;

        //What happens when trigger is pressed
        if (isSelectedSpell && SixenseInput.Controllers [0].Trigger != 0) {
            Debug.Log("Light spell");
            triggerIsPressed = true;
            wandLight.enabled = true;
            wandLight.intensity = (float)(SixenseInput.Controllers [0].Trigger) * 3;
            wandLight.range = (float)(SixenseInput.Controllers[0].Trigger) * 5;
        }  
        else {
            triggerIsPressed = false;
            wandLight.enabled = false;
        }

    //What happens when bumper is pressed
        if (isSelectedSpell && SixenseInput.Controllers [0].GetButtonDown (SixenseButtons.BUMPER) && triggerIsPressed == false) {
            Debug.Log("Light spell");
            var instantiateProjectile = Instantiate(lightProjectile, transform.position, transform.rotation) as Light;

            //instantiateProjectile.transform.TransformDirection(new Vector3(5,5,5));
        }
}

//When bumper button is pressed, the light flashes briefly
private void LightBurst(){
    wandLight.intensity = 1;

}

private void LightBurstActivated(){
    timeToDie = Time.time + life;
    transform.position = Camera.main.transform.position;
    transform.rotation = Camera.main.transform.rotation;
}

private void DeactivateLightBurst(){
    this.gameObject.SetActive(false);
}

private void LightBurstCountdown(){
    if(timeToDie < Time.time)
        DeactivateLightBurst();
}

private void MoveLight(){
    Vector3 velocity = speed * Time.deltaTime * transform.forward;
    transform.Translate(velocity * 3);
}

}

我得到的错误是:

NullReferenceException: Object reference not set to an instance of an object
Light_Spell.Update () (at Assets/Scripts/Spells/Light_Spell.cs:39)

感谢您的帮助

1 个答案:

答案 0 :(得分:1)

我认为这不是你得到的第一个错误。 游戏对象“WandLightSpell”没有灯光,或者没有找到任何具有该名称的游戏对象,并且您在Start()处获得了空引用异常。