- Home /
Solved by self
NullReferenceException. GetComponent not working?
I'm trying to write a map generator by having the different major functions in seperate scripts, and having one main one run the show. Problem is, my main script doesn't know the other ones exist.
In MainMapGen.js:
// Parent script for all map-generator stuff.
#pragma strict
//Instantiate all necessary subscripts.
function Start () {
var C_Level : Level = gameObject.GetComponent(Level);
var C_Sequences: Sequences = gameObject.GetComponent(Sequences);
var C_Sequencer: LevelSequencer = gameObject.GetComponent(LevelSequencer);
Debug.Log (C_Sequencer);
C_Sequencer.RunSequence('level1');
//Test code. Sequence level 1 no matter what.
}
In LevelSequencer.js:
#pragma strict
var sequence: String;
function RunSequence (sequence) {
Debug.Log('Testing switch:');
switch (sequence) {
case 'level1':
Debug.Log('Everything is ok so far.');
}
}
Both scripts are currently tacked to my main character. I have nearly identical calls in older scripts, but for some reason they work, this doesn't. I figured I made a stupid mistake somewhere, but there's no apparent syntactic differences. What am I doing wrong here?
The full error is: NullReferenceException: Object reference not set to an instance of an object MainMapGen.Start () (at Assets/Scripts/MapGen/MainMapGen.js:14)
Do you have the Level and Sequence scripts on your character? Also, JS and C# have occasional communication issues.
~ExplodingCookie
This should work. Are you sure that $$anonymous$$ain$$anonymous$$apGen and LevelSequencer (as well as the other scripts) are attached to the same object? It won't work if they are at different hierarchy levels (one at the parent, other at the child, for instance).
What line is your error on? You should always post the whole error, or else there are way too many places it could be :)
I didn't have all 4 sequences on him, but I tried that now and it didn't change anything. (Level/Sequences are blank while I try to make sure the framework itself works for what I'm trying to do). Checking the parenting, everything's on the same level. I had a 2nd copy of $$anonymous$$ain$$anonymous$$apGen on the model's cylinder, but removing that didn't fix. It just removed one instance of the error (which got thrown twice, so that was A problem at least.)
All my scripts are in js. I did that in hopes of not mashing langs together in my head and causing a veritable $$anonymous$$efield of syntax errors in the process. ^^;
The fill error in question is: NullReferenceException: Object reference not set to an instance of an object $$anonymous$$ain$$anonymous$$apGen.Start () (at Assets/Scripts/$$anonymous$$apGen/$$anonymous$$ain$$anonymous$$apGen.js:14)
And my Debug.Log above threw 'null', which AFAI$$anonymous$$ just tells me the same thing.
Follow this Question
Related Questions
NullReferenceException-Error when trying to GetComponent 3 Answers
Why is this null? Finding a script on an object 3 Answers
NullReferenceException. GetComponent dose not work properly 2 Answers
NullRef on my parent script 1 Answer
NullReferenceException: Object reference not set to an instance of an object ..... 1 Answer