• 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 /
  • Help Room /
This question was closed Jun 06, 2016 at 06:04 AM by Ian9921.
avatar image
Question by Ian9921 · Jun 04, 2016 at 06:10 AM · javascriptstringlevelstucklevels

Level counter gets stuck on the first phrase.

As you can tell by the title, I am making a level counter for a game. However, It always gets stuck on the first phrase which is "Tutorial". After that it is supposed to advance to "1" and then up from there. My code looks like this. On door to next level:

 static var statLevel : int;
 
 function OnTriggerEnter (){
 
     if (statLevel == null)
     {
         statLevel = 1;
     }
 
     statLevel ++;
 
     if (statLevel == 1)
     {
         Application.LoadLevel ("Challenge");
         MainScript.level = "Tutorial";
     }
 
     if (statLevel == 2)
     {
         Application.LoadLevel ("MageChallenge1");
         MainScript.level = "1";
     }
 
     if (statLevel == 3){
         Application.LoadLevel ("MageChallenge2");
         MainScript.level = "2";
     }
 
     if (statLevel == 4){
         Application.LoadLevel ("MageChallenge3");
         MainScript.level = "3";
     }
 
     if (statLevel == 5){
         Application.LoadLevel ("MageChallenge4");
         MainScript.level = "4";
     }
 
     if (statLevel == 6){
         Application.LoadLevel ("MageChallenge5");
         MainScript.level = "5";
     }
 
     if (statLevel == 7){
         Application.LoadLevel ("MageChallenge6");
         MainScript.level = "6";
     }
 
     if (statLevel == 8){
         Application.LoadLevel ("MageChallenge7");
         MainScript.level = "7";
     }
 
     if (statLevel == 9){
         Application.LoadLevel ("FinalTest");
     }
 }

And in my main script:

 static var deathLevel = "Challenge";
 static var level : String;
 public var deathMechanic : GameObject;
 static var die = true;
 static var isDying = false;
 
 static function deathActivate (){
     if (die == true)
     {
         if (deathLevel == "Challenge")
         {
             NextLevel.statLevel = 0;
             level = "Tutorial";
         }
 
         if (deathLevel == "MageChallengeChallenge1")
         {
             NextLevel.statLevel = 1;
             level = "1";
         }
 
         if (deathLevel == "MageChallengeChallenge2")
         {
             NextLevel.statLevel = 2;
             level = "2";
         }
 
         if (deathLevel == "MageChallengeChallenge3")
         {
             NextLevel.statLevel = 3;
             level = "3";
         }
 
         if (deathLevel == "MageChallengeChallenge4")
         {
             NextLevel.statLevel = 4;
             level = "4";
         }
 
         if (deathLevel == "MageChallengeChallenge5")
         {
             NextLevel.statLevel = 5;
             level = "5";
         }
 
         if (deathLevel == "MageChallengeChallenge6")
         {
             NextLevel.statLevel = 6;
             level = "6";
         }
 
         if (deathLevel == "MageChallengeChallenge7")
         {
             NextLevel.statLevel = 7;
             level = "7";
         }
 
         die = false;
         Instantiate(activeDeathMechanic, player.position, Quaternion.identity);
 
         yield WaitForSeconds (5);
 
         Application.LoadLevel (deathLevel);
     }
 }
 
 function Start ()
 {
     if (level == null)
     {
         level = "Tutorial";
     }
 
     die = true;
     if (NextLevel.statLevel == 1)
     {
         level = "Tutorial";
     }
     if (NextLevel.statLevel == 2)
     {
         level = "1";
     }
     if (NextLevel.statLevel == 3)
     {
         level = "2";
     }
     if (NextLevel.statLevel == 4)
     {
         level = "3";
     }
     if (NextLevel.statLevel == 5)
     {
         level = "4";
     }
     if (NextLevel.statLevel == 6)
     {
         level = "5";
     }
     if (NextLevel.statLevel == 7)
     {
         level = "6";
     }
     if (NextLevel.statLevel == 8)
     {
         level = "7";
     }
     if (NextLevel.statLevel == 9)
     {
         level = "Final";
     }
 }

 function Update (){
       if (NextLevel.statLevel == 2){
     deathLevel = "MageChallenge1";
 }

 if (NextLevel.statLevel == 3){
     deathLevel = "MageChallenge2";
 }

 if (NextLevel.statLevel == 4){
     deathLevel = "MageChallenge3";
 }

 if (NextLevel.statLevel == 5){
     deathLevel = "MageChallenge4";
 }

 if (NextLevel.statLevel == 6){
     deathLevel = "MageChallenge5";
 }

 if (NextLevel.statLevel == 7){
     deathLevel = "MageChallenge6";
 }

 if (NextLevel.statLevel == 8){
     deathLevel = "MageChallenge7";
 }

} Can anyone give me a logical reason as to why this doesn't work and a way to fix it? Your help will be greatly appreciated.

Comment

People who like this

0 Show 2
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 Alec-Slayden · Jun 04, 2016 at 09:41 AM 0
Share

I don't see where you are defining some of these variables, like "deathLevel". Is it giving compiler errors, or did you cut out some of the script declarations?

avatar image Ian9921 Alec-Slayden · Jun 05, 2016 at 03:43 AM 0
Share

I do have those variables, I just forgot to copy them in when i asked my question. I receive no compiler errors.

2 Replies

  • Sort: 
avatar image

Answer by Chris333 · Jun 04, 2016 at 10:14 AM

Hi,

int is a value-type, so its default value is 0, but you are checking the field statLevel of type int for null in the first if-clause of the first script. Null is the default value of reference types. Iam not sure if this also applies to js but in c# it works that way.

Comment

People who like this

0 Show 4 · 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 Wolfrik_Creations · Jun 04, 2016 at 04:02 PM 1
Share

Yes, in all coding languages (not just Unity) integurs and floating points can't be null.

avatar image Chris333 · Jun 04, 2016 at 06:16 PM 0
Share

In js variables can be undefined. Thats why i was not sure if the declaration of the int-type above in unity script would cause the variable to have the same behaviour as in c# or other strict typed languages or just stay as undefined. Thx for clarifying that wolfrik.

avatar image Wolfrik_Creations Chris333 · Jun 04, 2016 at 06:31 PM 0
Share

I'm not 100% sure about that but I'm pretty sure.

Though I do know that for instance when making a private/static variable that's an array, it will have to be declared as "new Type[]", integurs do not have to be declared when they are private/static.

avatar image Ian9921 · Jun 05, 2016 at 05:02 AM 0
Share

I have made some changes to account for this, but the level counter still gets stuck.

avatar image

Answer by skillbow · Jun 04, 2016 at 10:20 AM

I think some of this code can be tightened up a bit logically. It might not sort your problem but the code is currently open to some logical problems, for instance change:

 if (statLevel == null)
      {
          statLevel = 1;
      }

to

 if (statLevel <= 0)
      {
          statLevel = 1;
      }


Also change your long if statements to else ifs as if OnTriggerEnter is triggered within a short space of time, there is the possibility that statLevel will be incremented before the end of the function.

Have you used breakpoints or Debug.Log(statLevel) throughout the functions to see where it's failing?

Comment

People who like this

0 Show 5 · 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 Ian9921 · Jun 05, 2016 at 03:46 AM 0
Share

@skillbow I have added several Debug.Log statements to the code and all have triggered without fail. I placed one after every time I change the variable "level", although this still apparently stays at "Tutorial".

avatar image skillbow · Jun 05, 2016 at 07:47 AM 0
Share

Where are you setting deathLevel. It would be much easier to debug your code if you reduce the size a bit, for instance the Start function could be reduced to something like:

   die = true;
 
     if (level =="" || NextLevel.statLevel == 1 ) {
             level = "Tutorial";
      } else if (NextLevel.statLevel == 9) {
              level = "Final";
      } else {
         level = NextLevel.statLevel-1;
      }

Also can you post the code for your NextLevel class?

avatar image Ian9921 skillbow · Jun 05, 2016 at 07:32 PM 0
Share

NextLevel and MainScript are the names of the scripts that these variables are on. I am not sure if your code here would work, as level is a string and statLevel is an int, but I will give it a try.

avatar image skillbow Ian9921 · Jun 05, 2016 at 08:57 PM 0
Share

Just do

 int statLevelInt = NextLevel.statLevel -1;
 level = statLevelInt.ToString();

to convert to a string.

avatar image Ian9921 skillbow · Jun 05, 2016 at 08:20 PM 0
Share

Unfortunately, as I thought, this code did not work, even after quite a lot of modification.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

66 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

how to convert a dynamic playerpref to a dynamic int variable? 0 Answers

String Array = Anything? 1 Answer

Problem with Contains 0 Answers

PlayerPref doesnt work 2 Answers

How would I load a new scene when a score that is scored in a string is reached? 2 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