- Home /
Variable has not been assigned error
Hey. 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 {
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 () {
wandLight = GameObject.Find ("WandLightSpell").light;
wandLight.enabled = false;
triggerIsPressed = false;
}
// Handles button presses etc
void Update () {
if (SixenseInput.Controllers [0].Trigger != 0) {
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;
//wandLight.intensity = 0;
//wandLight.range = 0;
}
if (SixenseInput.Controllers [0].GetButtonDown (SixenseButtons.BUMPER) && triggerIsPressed == false) {
var instantiateProjectile = Instantiate(lightProjectile, transform.position, transform.rotation) as Light;
//instantiateProjectile.transform.TransformDirection(new Vector3(5,5,5));
}
}
Anybody see what I'm doing wrong here? Also the light is shooting upwards, how can I have it shoot wherever the wand is pointed at? Thanks!
It should also say which variable, and give a line number?
@OwenReynolds It is around line 40 where the instantiateProjectile business is happening. It says Light projectile
@OwenReynolds specific error is:
NullReferenceException: Object reference not set to an instance of an object Light_Spell.Update () (at Assets/Scripts/Spells/Light_Spell.cs:37)
This may not make any difference whatsoever, but I'm just wondering. Why are you instantiating a Light component and not a GameObject with a Light component attached?
@SimonLarsen I'm new to all this so I'm not sure why I would do it. What makes it better to do it that way? Do you mean a prefab? I made a light prefab and I have tried instantiate it and I think it fixed the error, however the light just floats above me and disappears not sure how to fix it
Answer by Owen-Reynolds · Mar 26, 2014 at 02:49 PM
I think it's just a matter of trying something a little too complicated, before you have more practice with Unity, and especially with coding. Maybe try something simpler first. Either another part of your project, or a simple version of this.
The line number you've got, 37, is a }. That probably means the code above is no longer the code you're running. After a bit, you'll get used to double-clicking on the errors, and what they mean.
There are a lot of examples how to use Instantiate. This one is a little long, and somewhat strange (the Start code looks for a pre-existing light, but the button code creates a new light?)
@OwenReynolds I understand what you mean. I'm still learning. Yeah I noticed that bit of odd code after you said it and removed it. However I don't know how to fix the instantiaten problem
Start with a working Instantiate, from the Unity docs. Then, when you change one thing and it stops working, you'll know it was that one thing.
Or turn what you have into a 6-line program. When you press the button, an (already turned on, etc...) light pops up. Once that works, add back in ai$$anonymous$$g, timeToDie... and test as you go.
Answer by Pablo_G · Mar 27, 2014 at 07:09 AM
After every "private" or "public" type in var ex. private var Light wandLight; public var Light lightProjectile;
That's for Unityscript. C# allows the word var, but if you already have the type, the var is just extra letters.
Your answer
![](https://koobas.hobune.stream/wayback/20220613141140im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
c# enum wont show in inspector 3 Answers
Input variable from Inspector? 1 Answer