Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by BeeVee · Mar 19, 2013 at 10:22 PM · variableinspectoroverride

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

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Mar 20, 2013 at 01:15 AM 1
Share

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 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Bunny83 · Mar 20, 2013 at 01:03 AM

@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# ;)

Comment
Add comment · Show 9 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Fattie · Mar 19, 2013 at 11:15 PM 0
Share

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 !

alt text

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

screen shot 2013-03-20 at 00.13.39.png (39.8 kB)
avatar image BeeVee · Mar 19, 2013 at 11:25 PM 0
Share

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?

avatar image BeeVee · Mar 20, 2013 at 12:02 AM 0
Share

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.

avatar image Chronos-L · Mar 20, 2013 at 12:41 AM 0
Share

@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 ];
 
    ....
 }
avatar image Bunny83 · Mar 20, 2013 at 01:08 AM 1
Share

@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.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

14 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges