- Home /
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);
}
}
}
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!
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;