- Home /
Null Reference Exception Error...specific error.
Hey folks! There's a problem with this script on the line I've indicated.
Just a simple coding error--with a really long, brute force script! I couldn't figure out how to put the scripts in an array, so I'm just doing this the hard way. But that's not the problem. The problem is that it's telling me there's a NullReferenceException, and that the Object Reference is not set to an instance of the object, and pointing to the first time I attempt to reference one of my scripts' Switch functions. All the scripts are the same, actually...just on eleven different objects. Print statements are telling me that the script that's giving me the error is Null...so I guess I'm not referencing any of this correctly?
I will tell tales of your heroism to all my friends in my design studio, if you can help me figure this one out. Thanks so much for your time. I feel stuck, right now...
Simon.
var maincamera:Camera; var eyeofbeholder : GameObject; var destroyer; destroyer = eyeofbeholder.GetComponent("Character Destroyer"); var wind_audio_object: GameObject; var backbeat_audio_object: GameObject; var banjo_audio_object: GameObject; var bass_audio_object: GameObject; var melody_audio_object: GameObject; var kick_and_hh_audio_object: GameObject; var tamborine_audio_object: GameObject; var snare_and_funk_audio_object: GameObject; var disco_hh_audio_object: GameObject; var mooooo_audio_object: GameObject; var boingy_audio_object: GameObject; var aloha_audio_object: GameObject;
var wind_audio_script; var backbeat_audio_script; var banjo_audio_script; var bass_audio_script; var melody_audio_script; var kick_and_hh_audio_script; var tamborine_audio_script; var snare_and_funk_audio_script; var disco_hh_audio_script; var mooooo_audio_script; var boingy_audio_script; var aloha_audio_script;
wind_audio_script = eyeofbeholder.GetComponent("Audio Switch"); backbeat_audio_script = eyeofbeholder.GetComponent("Audio Switch"); banjo_audio_script = eyeofbeholder.GetComponent("Audio Switch"); bass_audio_script = eyeofbeholder.GetComponent("Audio Switch"); melody_audio_script = eyeofbeholder.GetComponent("Audio Switch"); kick_and_hh_audio_script = eyeofbeholder.GetComponent("Audio Switch"); tamborine_audio_script = eyeofbeholder.GetComponent("Audio Switch"); snare_and_funk_audio_script = eyeofbeholder.GetComponent("Audio Switch"); disco_hh_audio_script = eyeofbeholder.GetComponent("Audio Switch"); mooooo_audio_script = eyeofbeholder.GetComponent("Audio Switch"); boingy_audio_script = eyeofbeholder.GetComponent("Audio Switch"); aloha_audio_script = eyeofbeholder.GetComponent("Audio Switch");
var flower:Transform; var bee:Transform; var cloud:Transform; var butterfly:Transform; var sign:Transform; var mesa:Transform; var mushroom:Transform; var tree:Transform; var cow:Transform; var rock:Transform; var rainbow:Transform;
var music_level = 0; var loop_length = 18; var number_of_objects = 0; var mybeat; var timecount = 0.00; var currentbeat = 0.00;
function Start() { StartLoop(); }
function StartLoop() { number_of_objects = destroyer.combocount; if(number_of_objects >= 1) { Invoke("Flower", .75); } if(number_of_objects >= 2) { Invoke("Bee", .75); } if(number_of_objects >= 3) { Invoke("Cloud", .75); } if(number_of_objects >= 4) { Invoke("Butterfly", .75); } if(number_of_objects >= 5) { Invoke("Sign", .75); } if(number_of_objects >= 6) { Invoke("Mesa", .75); } if(number_of_objects >= 7) { Invoke("Mushroom", .75); } if(number_of_objects >= 8) { Invoke("Tree", .75); } if(number_of_objects >= 9) { Invoke("Cow", .75); } if(number_of_objects >= 10) { Invoke("Rock", .75); } if(number_of_objects >= 11) { Invoke("Rainbow", .75); } if(music_level != number_of_objects || number_of_objects == 0) { switch(music_level) { case 0:
------------------> ERROR POINTS TO NEXT LINE <---------------- wind_audio_script.Switch();
break; case 1: backbeat_audio_script.Switch(); break; case 2: banjo_audio_script.Switch(); break; case 3: bass_audio_script.Switch(); break; case 4: melody_audio_script.Switch(); break; case 5: kick_and_hh_audio_script.Switch(); break; case 6: tamborine_audio_script.Switch(); break; case 7: snare_and_funk_audio_script.Switch(); break; case 8: disco_hh_audio_script.Switch(); break; case 9: mooooo_audio_script.Switch(); break; case 10: boingy_audio_script.Switch(); break; case 11: aloha_audio_script.Switch(); break; } music_level = number_of_objects; switch(music_level) { case 0: wind_audio_script.Switch(); break; case 1: backbeat_audio_script.Switch(); break; case 2: banjo_audio_script.Switch(); break; case 3: bass_audio_script.Switch(); break; case 4: melody_audio_script.Switch(); break; case 5: kick_and_hh_audio_script.Switch(); break; case 6: tamborine_audio_script.Switch(); break; case 7: snare_and_funk_audio_script.Switch(); break; case 8: disco_hh_audio_script.Switch(); break; case 9: mooooo_audio_script.Switch(); break; case 10: boingy_audio_script.Switch(); break; case 11: aloha_audio_script.Switch(); break; } } Invoke("StartLoop",loop_length); }
function Flower() { mybeat = Instantiate (flower, Vector3(-20, 3, transform.position.z+maincamera.farClipPlane-.5), Quaternion.identity); mybeat.tag = "Flower"; }
function Bee() { mybeat = Instantiate (bee, Vector3(-20, 3, transform.position.z+maincamera.farClipPlane-.5), Quaternion.identity); mybeat.tag = "Bee"; }
function Cloud() { mybeat = Instantiate (cloud, Vector3(-20, 3, transform.position.z+maincamera.farClipPlane-.5), Quaternion.identity); mybeat.tag = "Cloud"; }
function Butterfly() { mybeat = Instantiate (butterfly, Vector3(-20, 3, transform.position.z+maincamera.farClipPlane-.5), Quaternion.identity); mybeat.tag = "Butterfly"; }
function Sign() { mybeat = Instantiate (sign, Vector3(-20, 3, transform.position.z+maincamera.farClipPlane-.5), Quaternion.identity); mybeat.tag = "Sign"; }
function Mesa() { mybeat = Instantiate (mesa, Vector3(-20, 3, transform.position.z+maincamera.farClipPlane-.5), Quaternion.identity); mybeat.tag = "Mesa"; }
function Mushroom() { mybeat = Instantiate (mushroom, Vector3(-10, 1.7, transform.position.z+maincamera.farClipPlane), Quaternion.identity); mybeat.tag = "Mushroom"; }
function Tree() { mybeat = Instantiate (tree, Vector3(-10, 1.7, transform.position.z+maincamera.farClipPlane), Quaternion.identity); mybeat.tag = "Tree"; } function Cow() { mybeat = Instantiate (cow, Vector3(-10, 1.7, transform.position.z+maincamera.farClipPlane), Quaternion.identity); mybeat.tag = "Cow"; }
function Rock() { mybeat = Instantiate (rock, Vector3(10, 1.7, transform.position.z+maincamera.farClipPlane+1), Quaternion.identity); mybeat.tag = "Rock"; }
function Rainbow() { mybeat = Instantiate (rainbow, Vector3(10, 1.7, transform.position.z+maincamera.farClipPlane+1), Quaternion.identity); mybeat.tag = "Rainbow"; }
Answer by Cyb3rManiak · Jun 02, 2011 at 09:17 AM
This is way too much code to go over, but fortunately it's probably a pretty simple solution.
The part where you find the components:
wind_audio_script = eyeofbeholder.GetComponent("Audio Switch");
backbeat_audio_script = eyeofbeholder.GetComponent("Audio Switch");
etc...
Is outside of any method. That area is for declaring variables, and unless you know what you're doing, you're not going to want to call any methods there. The behaviour will be unpredictable.
Simply put - you can do
var wind_audio_script; // Since it's only a declaration
or
var myNumber = 5; // Since you're assigning a constant value
But you shouldn't do
wind_audio_script = eyeofbeholder.GetComponent("Audio Switch");
Since GetComponent() is a method. Think about it - when exactly will it run? In the editor? When you press play? Before or after the Start() method?
What you should be doing, is calling all of these methods in your Start() or Awake() method. That's exactly what they are for.
So move all of your lines that use methods (in your case GetCompoenent()) like this:
...
var wind_audio_object: GameObject;
var backbeat_audio_object: GameObject;
...
function Start()
{
Initialize();
StartLoop();
}
function Initialize()
{
...
wind_audio_script = eyeofbeholder.GetComponent("Audio Switch");
backbeat_audio_script = eyeofbeholder.GetComponent("Audio Switch");
...
}
...
And as a side note - read some example scripts, and scripts posted by other people to structure your code better, and learn to use the tools the programing language gives you. I promise it will help you in the long run. No need to bruit force anything. You can't maintain scripts like this for a long time. Eventually they will become too big and complicated to handle.
I really appreciate this. That's exactly what was wrong. And I only found out a day later...I wish I had done this the easy way. As far as learning not to brute force it, I think I just need to do what you're saying. Thanks so much!
Your answer
Follow this Question
Related Questions
How to access a second script on same object 1 Answer
Accessing components through a collision 0 Answers
Need Help With a NullReference Exception 2 Answers
NullReferenceException 1 Answer
Null Reference Exception Hunt ! 1 Answer