- Home /
Why isn't the Inspector overriding my variable?
Hi,
I have written a script to jump from one level to the next, thinking that I could set it using the inspector to override the variable I set at the start of the script, but the script keeps using the preset variable in the script, rather than what I write in the Inspector. Here's my code:
var levelToLoad = "Level_02";
function Start(){
}
function Update () {
}
function OnTriggerEnter() {
transform.parent.gameObject.audio.Play();
yield WaitForSeconds(3.0);
Application.LoadLevel(levelToLoad); // Will load Level specified in the inspector view for this game object
}
In the Inspector, I write "Level_03", but it still goes to Level_02.
All the levels are in the Build Settings, and the same thing happens whether I use the Level names, or the Build index numbers.
Any advice?
B
Actually i don't see a problem with the script itself. However there might be a problem that the gameobject from the previous scene is still there because you used DontDestroyOnLoad or something like that.
Since you have a seperate instance of this script in each scene, you could try to rename the gameobject thescript is attached to to the level name. Then add this line at the beginning of your OnTriggerEnter function:
Debug.Log("End of level: " + gameObject.name + " Loading new level: " + levelToLoad);
Try it and tell us what happens.
Answer by Bunny83 · Mar 20, 2013 at 01:03 AM
If the levels are linear you would do something like:
Application.LoadLevel(Application.loadedLevel + 1);
Of course after the last level there should be the win level.
Btw you mixed UnityScript and C# ;)
BeeVee -- just don't use the Inspector to set things - do not use public variables.
They're just for little kids and you should never use that feature :)
Now that you've explained you have multiple levels etc, just don't use the Inspector variable setting at all. $$anonymous$$ark all variables private.
it is a common problem that you're changing the value in your script (like var count:int=8 or something), and you've forgotten that you did not say "private" .. and then the editor keeps forcing it back to whatever value you first had there. So don't use the Unity Inspector-setting.
In this particular situation with multiple levels, don't use it.
Useful tip:
What if you actually want to have a 'real" public variable (ie, that other scripts can access) and you don't want it to be ruined by Unity's Inspector-setting-thingy? The answer is
@System.NonSerialized
var overall$$anonymous$$PS:float;
Essentially, other than just in HelloUnity scripts, every time you have a public variable, you need to mark it nonserialized, like that.
(It's somewhat annoying really, I'd prefer you could set some sort of master flag where the default is the other way - but no problem.)
just don't set it in the script. (ie just go var levelToLoad:String; and that's it)
TBC this advice is only relevant if you're not trying to do "multiple uses of the script," "multiple scenes," etc.
Also .. click the little-known Reset function !
I had utterly no idea about this until Eric5.5 mentioned it on this very site!
Note Bunny has pointed out there is actually a Reset() function that's called .. amazing!
ScriptReference/$$anonymous$$onoBehaviour.Reset.html
Aha! That half-worked, but I don't seem to be able to set the Inspector on different scenes to different values? I can set it and it now works, but it changes all of the Goals to the same next scene. What I wanted to do, perhaps it's not possible, is to use the Inspector to set each next level separately? So the OnGoal script for Level_01 would have Level_02 for the levelToLoad variable; Level_02 would have Level_03 for the var; Level_03 would have Level_04 and so on? Can I do this?
I thought I might be able to get around this all by declaring all my levels and what levels they should go to, but as someone who hasn't coded since the Amiga all I could come up with was a series of pseudocode-like else ifs that look horrible and will probably not work, even if I can clear out all the errors they generate in this form:
if(Application.loadedLevelName == Level_01);
var nextLevel = "Level_02";
else if(Application.loadedLevelName == Level_02);
var nextLevel = "Level_03"
else if(Application.loadedLevelName == Level_03);
var nextLevel = "Level_04"
else if(Application.loadedLevelName == Level_04);
var nextLevel = "Winner"
then I could just call nextLevel ins$$anonymous$$d of levelToLoad in the code above.
@beevee, if your game is linear, then:
var levelNames : string[] = { "Level_01"
, "Level_02", "Level_03", "Level_O4", "Winner" };
int currentLevelIndex = ArrayUtility.IndexOf( levelNames, Application.loadedLevelName );
if( currentLevelIndex < levelNames.length - 1 ) {
var nextLevel = levelNames[ currentLevelIndex + 1 ];
....
}
@Fattie: Actually that shouldn't make any difference. Unity deserializes the scene data after the field initializers and the constructors have been executed. So it doesn't matter ic you set an initial value in the field initializer. However Awake and Start are called after the deserialization so it would overwrite the value you set in the inspector.
Btw, just because you mentioned the Reset button. There is a callback when you press this button. Reset function. This get called automatically when the script is attached to a gameobject or when the Reset button is pressed.
Your answer
![](https://koobas.hobune.stream/wayback/20220613094724im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Notes or Bubble Window for Variables in Inspector Giving Help? 3 Answers
Script with variable number of variables ? 1 Answer
How to use different types of scripts with an override function. 2 Answers
UnassignedReferenceException when its already assigned 1 Answer
Invisible Public Variable in Inspector 5 Answers