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.

1 Answer

1

@Chronos-L:

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# :wink:

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?

@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 ]; .... }

@bunny83, I can't believe that I missed that. I am so used to C# now, I can't write in uJS anymore, I instinctively write the codes in my head in C#.

@Bunn - had no clue there was a Reset() function! Unreal, thanks! How does anyone know this stuff? :)

"but I don't seem to be able to set the Inspector on different scenes" you can't dude, add private and handle it in code Dude I HATE elsif ... never use elsif !!!!!!!!!!!! Arrrg!