- Home /
Unassigned Reference Exception error?
Hey. So I have managed to shoot a light from a wand in my game, it works okayish for now, however in the console I keep getting this error:
UnassignedReferenceException: The variable projectile of 'Light_Spell' has not been assigned.
You probably need to assign the projectile variable of the Light_Spell script in the inspector.
UnityEngine.Object.Internal_InstantiateSingle (UnityEngine.Object data, Vector3 pos, Quaternion rot)
And the line of code the error points to is this:
Rigidbody instantiateProjectile = Instantiate(projectile, transform.position, transform.rotation) as Rigidbody;
How can I solve this? Its probably some dumb error but I'm new to Unity so not sure what it could be.
You should not approach errors like they are dumb, unless you are %100 sure that you know the cause and it is really dumb. All errors must be fixed in sight :).
This error causes because projectile property of Light_Spell script is null. How do you assigning projectile property of Light_Spell?
Answer by grebenyuksv · Jan 12, 2015 at 04:45 PM
When I had the same error, the reason was that I had accidentally dragged the script to a wrong object in the scene view, after which I had attached the script to the correct object. Sounds like a joke, but nontheless, check if your script is attached to any unwanted object.
had the same prob. but yes, mistakenly assigned the script to the wrong object. thanks it worked. +1
Is there any quick way to check this or do I have to go through each individual one?
This was it for me. Except it wasn't an accident, I just thought that unchecking the script on the other object would mean that it no longer affects anything. Apparently not.
Answer by Gooey-Man · Mar 12, 2014 at 12:11 AM
Did you assign the variable to the script its self or the object that the script is on? If you assigned it to the script assign it to the object the script is on instead.
I have a WandLight that has the LightSpell script attached to it and that has the Light_Shot prefab put into it. I just dragged and dropped the prefab onto it
Answer by wpatel · Feb 26, 2020 at 08:57 PM
In the event someone ventures here in 2020, I've seen this error occur on not one but tens of prefabs (if not all) due to a Unity version upgrade.
Opening and resaving the prefab is one way to resolve it.
Deleting your Library folder was the way we solved it.
Unity regenerates the folder and everything worked.
Why this occurred for the project I was working on?
The project was stored on a git repo.
One user upgraded the project to Unity 2019 (from 2018) and committed those changes.
A user who pulled down those changes and opened up the project in 2019.
No migration was needed so Unity didn't do any "cleanup" to project files.
Hit Play. Lots of Unassigned Reference Exceptions.
Don't know the dirty details of what happened but fortunately the prefab files themselves were not corrupted.
Deleting the Library\ folder did not work for me.
In my $$anonymous$$onoBehaviour, I have a public List of GameObjects that is uninitialized (null).
public List<GameObject> people;
I Initialize it in both Awake() and Start() as follows:
if (null == people) {
people = new List<GameObject>();
}
I will get an error like this: UnassignedReferenceException: The variable people of Room has not been assigned. You probably need to assign the people variable of the Room script in the inspector.
This was a prefab that used to work in earlier versions of Unity3D. So, maybe List needs to be assigned in the inspector rather than created on the fly in the script now?
I will try some experiments and maybe report back later if I remember.
Answer by Ranger-Ori · Oct 06, 2021 at 02:08 PM
grebenyuksv said, that he didn't notice since he accidentaly dragged the script to the wrong object.
It can be easily fixed if you press on FindAllReferencesInScene for a certain script (right click on script).
I had a much dumber issue.
I accidentaly dragged the script to the correct object, twice. (facepalm)
It was hidden within other components in the inspector, and FindReferences pointed me to only one gameobject.
Your answer
Follow this Question
Related Questions
Advanced runtime lighting calculation 0 Answers
How to refresh rigidbody colliders modified at runtime 0 Answers
Connect two rigidbody gameobjects together at runtime? 0 Answers
Change screen orientation in runtime 0 Answers
UnassignedReferenceException 1 Answer