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 ekeegan · May 06, 2015 at 12:46 PM · levelload

Show Button to LoadLevel After Collecting # of Points

Hello Unity community! I am working with the roll-a-ball game adding my own twists here and there. One of the problems I have run into for this maze game is having a proper way to load a new level. I would like to collect a certain amount of points then activate a LoadScene button script based off a count so my player can choose to load the next level.

Below is my script on my player:

C# using UnityEngine; using UnityEngine.UI; using System.Collections;

 public class PlayerController : MonoBehaviour {
     
     public float speed;
     public Text countText;
     public Text winText;
     
     private Rigidbody rb;
     private int count;
 
 
 
     void Start ()
     {
         rb = GetComponent<Rigidbody>();
         count = 0;
         SetCountText ();
         winText.text = "";
     }
     
     void FixedUpdate ()
     {
         float moveHorizontal = Input.GetAxis ("Horizontal");
         float moveVertical = Input.GetAxis ("Vertical");
         
         Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
         
         rb.AddForce (movement * speed);
     }
     
     void OnTriggerEnter(Collider other) 
     {
         if (other.gameObject.tag == "Pick Up")
         {
             other.gameObject.SetActive (false);
             count = count + 1;
             SetCountText ();
         }
     }
     
     void SetCountText ()
     {
         countText.text = "Artifacts Found: " + count.ToString ();
         if (count >= 1)
         {
             //winText.text = "You Win!";
             gameObject.GetComponent<LoadScene>().enabled = true;
         }
 
 
     }
 
 }

And the other script trying to be called on count is on the camera

 using UnityEngine;
 using System.Collections;
 
 
 public class LoadScene : MonoBehaviour {
     
     void OnGUI () {
 
         gameObject.GetComponent<LoadScene>().enabled = false;
 
         // Make a background box
         GUI.Box(new Rect(10,10,100,90), "Loader Menu");
         
         // Make the first button. If it is pressed, Application.Loadlevel (1) will be executed
         if(GUI.Button(new Rect(20,40,80,20), "Level 1")) {
             Application.LoadLevel(1);
         }
         
         // Make the second button.
         if(GUI.Button(new Rect(20,70,80,20), "Level 2")) {
             Application.LoadLevel(2);
         }
     }
 
 }

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 ekeegan · May 07, 2015 at 06:25 AM 0
Share

Wow thank you! This worked quite well and was very thorough, sorry for my inability to better explain. I've been investigating timers and loading bars as well but this solution works for my purpose until I piece together a script for using yield waitforseconds and application loadlevel. The initial problem is just trying to load a new level in a timely manner once the player collects x amount of points. Switching scenes immediately is too jarring. Thanks again!

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by hbalint1 · May 06, 2015 at 01:07 PM

You didn't said what is the problem. If it's NullReferenceException, i can see this:

 gameObject.GetComponent<LoadScene>().enabled = true;

The GetComponent method only searches the given GameObject it is on. So if you call this it will just look for the script on the player. But you are trying to get a script from the camera, so you need to search it first:

 GameObject.Find("yourCamera'sName").GetComponent<LoadScene>().enabled = true;


But the proper way of doing this in my opinion is to make boolean variables in the LoadScene script for the levels (this can be done by making individual variables or make an array). When you collected the right amount of points, you can set the boolean to true for showing the button.

 using UnityEngine;
  using System.Collections;
  
  
  public class LoadScene : MonoBehaviour {

      public bool level1 = false;
      public bool level2 = false;

      void OnGUI () {
  
          // Make a background box
          if(level1 || level2)
              GUI.Box(new Rect(10,10,100,90), "Loader Menu");
          
          // Make the first button. If it is pressed, Application.Loadlevel (1) will be executed
          if(level1)
              if(GUI.Button(new Rect(20,40,80,20), "Level 1"))
                  Application.LoadLevel(1);

          // Make the second button.
          if(level2)
              if(GUI.Button(new Rect(20,70,80,20), "Level 2"))
                  Application.LoadLevel(2);
      }
  
  }

and when the collected points are enough for level1, you can say:

 GameObject.Find("yourCamera'sName").GetComponent<LoadScene>().level1 = true;

in this way you can enable the buttons one by one. (the LoadScene component must be active)

by the way i don't know what are you trying to achieve with this: gameObject.GetComponent().enabled = false;

Comment
Add comment · Show 1 · 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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Check loaded level if statement 1 Answer

Call method after loading scene? 1 Answer

How do I stop the scene from switching when the gameObject is destroy / 0 Answers

Help with input code please (c#) 1 Answer

Keeping objects destroyed during runtime of the game 1 Answer


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