I CAN'T LOAD LEVEL! - (SOLVED)

     var Paper : int = 0;
     var paperToWin : int = 30;        //number to win!
   
     function OnTriggerEnter( other : Collider )
      {
            if (other.gameObject.tag == "Key")
            {
                     Paper += 1;
                     Debug.Log("A key was picked up. Total keys = " + Paper);
                     Destroy(other.gameObject);
             }
      }
    
     function OnGUI()
     {
         if (Paper < paperToWin)
         {
             GUI.Box(Rect((Screen.width/2)-100, 10, 200, 35), "" + Paper + " Keys");
         }
         else
         {
             GUI.Box(Rect((Screen.width/2)-100, 10, 200, 35), "All Keys Collected!");
             Application.LoadLevel ("ThanksForPlaying(limited build)");
         }

BTW I am new to scripting
I’ve barley done it and I have used and edited other peoples scripts, like this one. I replaced some lines of code to make it say Key instead of paper and I added the 23 line of code. I probably did something wrong with the line of code that I added but I hope you will help me.

Honestly, I would get away from using OnGUI and switch to the new UI system instead.

And, depending on the version of Unity you are on, Application.LoadLevel might be deprecated and instead you have to use the SceneManager.

From what I recall of OnGUI is it’s called every frame I believe which means it’s possible that it’s calling Application.LoadLevel several times. This is a guess as I haven’t used OnGUI in ages. But this could explain your lag.

Yes, Application.LoadLevel is deprecated in unity 5.
You should use SceneManager.LoadScene(“MySceneName”); instead.

If you’re new to coding, start by looking up the basic unity tutorial.

Or even to search for basic programming courses.
You won’t go far if you don’t know how to code on a basic level and starting by unity isn’t going to be the best way to learn imho.

Also, you should use C#.

Hello, I tried to put switch out the code for the scene manager script but all that it did is give me this error

Assets/objectcounter.js/ObjectCounter.js(23,14): BCE0005: Unknown identifier: ‘SceneManager’.

here is the new script as well

     var Paper : int = 0;
     var paperToWin : int = 30;        //number to win!
   
     function OnTriggerEnter( other : Collider )
      {
            if (other.gameObject.tag == "Key")
            {
                     Paper += 1;
                     Debug.Log("A key was picked up. Total keys = " + Paper);
                     Destroy(other.gameObject);
             }
      }
    
     function OnGUI()
     {
         if (Paper < paperToWin)
         {
             GUI.Box(Rect((Screen.width/2)-100, 10, 200, 35), "" + Paper + " Keys");
         }
         else
         {
             GUI.Box(Rect((Screen.width/2)-100, 10, 200, 35), "All Keys Collected!");
             SceneManager.LoadScene("ThanksForPlaying(limited build)");
         }
     }

Hello, I tried to put switch out the code for the scene manager script but all that it did is give me this error

Assets/objectcounter.js/ObjectCounter.js(23,14): BCE0005: Unknown identifier: ‘SceneManager’.

here is the new script as well

     var Paper : int = 0;
     var paperToWin : int = 30;        //number to win!
   
     function OnTriggerEnter( other : Collider )
      {
            if (other.gameObject.tag == "Key")
            {
                     Paper += 1;
                     Debug.Log("A key was picked up. Total keys = " + Paper);
                     Destroy(other.gameObject);
             }
      }
    
     function OnGUI()
     {
         if (Paper < paperToWin)
         {
             GUI.Box(Rect((Screen.width/2)-100, 10, 200, 35), "" + Paper + " Keys");
         }
         else
         {
             GUI.Box(Rect((Screen.width/2)-100, 10, 200, 35), "All Keys Collected!");
             SceneManager.LoadScene("ThanksForPlaying(limited build)");
         }
     }

Your “Paper” value has probably been serialized to a higher value than zero. Do not rely on the initialization values to define starting values, because Unity can mess with those values due to how components are serialized.

You should be using function Start() to set Paper = 0.

function Start()
{
    Paper = 0;
}

SceneManager is located in the UnityEngine.SceneManager namespace.
You have to import it at the top of your file.

import UnityEngine.SceneManager

@Kasper_OmsK answered the question about SceneManager already.

Part of the issue is you should get away from OnGui and use the newer UI system. Then, you should call a function when you collect Paper which will check if it’s >= paperToWin and if it is, display the proper text and then try to load the level.

I don’t use OnGUI either, but when I popped in this thread and saw the OnGUI event followed by an if/else branch where the level was being loaded in an else that would be triggered all the time at the start of the game, my eyes bulged out of my head. Pretty sure that would infinitely load the level, essentially hanging the game.

ok thank you to everyone! it really helps. You’ve all helped me solve my problem! thank you so much!

Thank you everyone! The problem has been solved. Thank you all again for being so helpful to me!