The answer is always the same… ALWAYS. It is the single most common error ever.
Don’t waste your life spinning around and round on this error. Instead, learn how to fix it fast… it’s EASY!!
Some notes on how to fix a NullReferenceException error in Unity3D
also known as: Unassigned Reference Exception
also known as: Missing Reference Exception
also known as: Object reference not set to an instance of an object
http://plbm.com/?p=221
The basic steps outlined above are:
Identify what is null
Identify why it is null
Fix that.
Expect to see this error a LOT. It’s easily the most common thing to do when working. Learn how to fix it rapidly. It’s easy. See the above link for more tips.
This is the kind of mindset and thinking process you need to bring to this problem:
Let me take you step by step through it.
You see the error line, line 35?
The only possible thing that could be null in that line is audio_BGMusic
So that's part 1. That's what's null. We identified it.
Part 2... WHY is it null? Well, first we start with "who is supposed to make it not null?"
Looking up further I see line 19 sets it, with this construct:
audio_BGMusic = ObjectMusic.GetComponent<AudioSource>();
So lets take that apart. How can that fail? We have to suppose it DID fa…
Step by step, break it down, find the problem.
Here is a clean analogy of the actual underlying problem of a null reference exception:
Is your particle system assigned to the field in the Inspector on your script?
A Null Reference Exception (NRE) is caused when you’re trying to access a method or property on a null object – in other words, trying to get something from nothing.
Pretend I give you a set of instructions:
I will hand you a package of Oreos.
Open the package
Take out a cookie.
Eat the cookie.
Close the package.
I hand you the package, you open it, and the container is empty. Your “code” would fail at line 4, be…
If you post a code snippet, ALWAYS USE CODE TAGS:
How to use code tags: Using code tags properly
How to report your problem productively in the Unity3D forums:
http://plbm.com/?p=220
How to understand compiler and other errors and even fix them yourself:
Errors are there to help you. It’s a good idea to try and learn how to understand them. Try to apply the breakdown in this post of your original error to the new error you’re receiving. If you don’t understand a word, don’t just gloss over it. Ask what the word means here, or research it elsewhere. Cultivating a habit of research will pay dividends for you in every part of your coding journey.
Here’s a breakdown of your specific error, but try applying this process to any other error you may en…