0

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

rafaelc
  • 57,686
  • 15
  • 58
  • 82
Ayohaych
  • 5,099
  • 7
  • 29
  • 51
  • possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – CodeSmile Feb 16 '15 at 10:43

1 Answers1

1

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().

Roberto
  • 11,557
  • 16
  • 54
  • 68
  • In my console it just shows that error loads. I have the script setup as public and dragged it over in the inspector but its still saying it can't find it and I have no idea why – Ayohaych Mar 25 '14 at 23:14
  • 1
    It's strange that the reference is null but it was successfully assigned during Start(). The only options are that there's something destroying the component/game object or it's being deassigned after Start(). What exactly is in the line of error (line 36)? – Roberto Mar 25 '14 at 23:43
  • Yeah I know right. I updated the OP with the new code I have as I updated some things. The error is as follows: NullReferenceException: Object reference not set to an instance of an object Light_Spell.Update () (at Assets/Scripts/Spells/Light_Spell.cs:39) And the line of code is: selectedSpellNum = changespell.GetSelectedSpellNumber(); – Ayohaych Mar 25 '14 at 23:55
  • 1
    changeSpell is null because you commented changespell = currentSpellLight.GetComponent(); – Roberto Mar 26 '14 at 00:09
  • Yeah but I have public ChangeSpell changespell set and I dragged and dropped the game object with that script on it into the inspector and it didn't work – Ayohaych Mar 26 '14 at 00:11