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.

1 Like

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?

–

4 Answers

4

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.

–

give this man an award

–

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.

2025 and u still saved my ass, thnks

–

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

–

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.