- Home /
Spooky NullReferenceException MYSTERY
EDIT: Someone knows what's wrong with this, but I don't understand what they're trying to tell me! Can you guys explain to to me?
Hey, folks!
So, I'm having a heck of a time trying to figure out why my code isn't compiling. It's telling me that the Object Reference is not set to an instance of an object...but i've assigned all the variables in the inspector, and I know that the syntax for the variables in the scripts I'm referencing are correct. The syntax for the script I'm referencing is also correct. But it still tells me that the placer script is null when I put in a print statement.
It points to the first if statement, where I'm first referring to "placer", when the error pops up. It might not mean anything, but the object that this script is placed on is parented to another object which is parented to the first person controller, which is where the placer script is...that wouldn't change anything, would it?
Please, any help on understanding this would be really appreciated!
Spooked,
Simon
UPDATE: I've discovered that half the time it's telling me that it knows what placer.number_of_objects is, and half the time it doesn't...
var eyeofbeholder : GameObject;
var placer;
placer = eyeofbeholder.GetComponent("Music Placer");
var thislevel: int;
var switched = false;
function Awake()
{
audio.volume = 0.0;
}
function Update()
{
if((!switched && placer.number_of_objects==thislevel && (placer.music_level != placer.number_of_objects || placer.number_of_objects == 0)) || placer.music_level == thislevel)
{
switched = true;
placer.music_level = placer.number_of_objects;
Switch();
}
}
function Switch()
{
if(audio.volume == 1.0)
{
audio.volume = 0.0;
}
else
{
audio.volume = 1.0;
}
}
function Reset()
{
switched = false;
}
ah, thanks, almo! Now, if only I could figure out what the problem was....I'd be a happy man.
Take a screenshot of the inspector where you've assigned something to the 'placer' variable. Post this screenshot to imgur.com and come back with the link.
Okay, cool...so here's the first picture I've uploaded...of the object that the script is assigned to. Next I'll upload a photo of the script I'm trying to reference, the music placer script. But the "placer" variable is just defined in the script, right? It's just a script on the eyeofbeholder object.
Answer by Catlard · Jun 03, 2011 at 03:53 PM
I totally figured out what was wrong with this. It was a straight up situation where I was trying to call something outside of any method, so when the awake functions were running, they weren't finding it the variables they were looking for. Grrr.
Ahh!! I code all in C# and I didn't remember if stuff outside of functions in Javascript would implicitly be included in Start() or Awake() or something. Wow.. sorry for the wild goose chase.
On the contrary, my good man...without you, I wouldn't even be chasing a goose. Just my own butt. Thanks for all your help! It made a big difference to know I wasn't alone in the coding universe, if nothing else.
Answer by Catlard · Jun 02, 2011 at 07:51 PM
I'm totally psyched that you see what's wrong, but I don't quite understand what you mean. Music Placer is a script, so it can't have a component, right? So how can I have a First person controller on it?
Are you talking about in the script, or in the inspector? I'm not seeing what you've noticed.
Sorry!
Simon
Answer by flaviusxvii · Jun 02, 2011 at 07:15 PM
Ahh.. eyeOfTheBeholder is a FirstPersonController.. it doesn't have a "Music Placer" component.
Hmm...it's a good thought, and I jumped to check my First Person Controller for the music placer script, but to me in the picture it looks like it does....that's what's centered in the inspector in the second picture, right?
You have a First Person Controller in your $$anonymous$$usic Placer.. not the other way around.
I'm totally psyched that you see what's wrong, but I don't quite understand what you mean. $$anonymous$$usic Placer is a script, so it can't have a component, right? So how can I have a First person controller on it?
Are you talking about in the script, or in the inspector? I'm not seeing what you've noticed.
Sorry!
Simon