Pergunta

I'm trying to shoot a light from a magic wand, and I keep getting this error when I run the game. The light still comes out but the error appears in the console. I created a light prefab and I dragged that onto my script "Light_Spell". Here is the code:

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);
}

}

The error I get is:

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

Thanks for any help

Foi útil?

Solução

I think this is not the very first error you are getting. The game object "WandLightSpell" doesn't have a light or it didn't find any game object with that name and you are getting null reference exceptions at the Start().

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top