I made 6 pickup objects in my scene. At the end of the level there is a gameObject with Application.LoadLevel();. How do I make it so the Application.LoadLevel(); script is disabled BEFORE the player picks up all the pickup objects and enabled AFTER the player picks up all the pickup objects. Example: player picks up 5 pickup objects - script is disabled and player then picks up the 6th pickup object - script is immediately enabled.
Pickup script:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class PickUp : MonoBehaviour {
public Text countText;
private int count;
// Use this for initialization
void Start ()
{
count = 0;
countText.text = count.ToString () + "/6" ;
SetCountText ();
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag ("Pick Up"))
{
other.gameObject.SetActive (false);
count = count + 1;
SetCountText ();
}
}
void SetCountText ()
{
countText.text = count.ToString () + "/6" ;
}
}
In this case you can use a Static variable, but this is bad practice in general. Rather create a manager that keeps track of all pickups.
But anyway here you go:
public Text CountText;
public int CountBeforeLevelLoad = 6;
// The use of a static variable is pretty dirty, best practice is to make an external manager or other entity to keep track of your picked up items!
// But it does the trick in your case ;-)
protected static int Count;
protected void Start()
{
Count = 0;
SetCountText();
}
protected void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Pick Up"))
{
other.gameObject.SetActive(false);
Count++;
SetCountText();
if (Count == CountBeforeLevelLoad)
{
// Load your level
}
}
}
protected void SetCountText()
{
CountText.text = Count + "/6";
}
I figured it out! Thanks to all who answered, I really appreciate the help.
I simply added public int myObject;, added myObject.SetActive (false); in void Start () and added if (count ==6) { myObject.SetActive (true); } in void Update ()
The new script:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class PickUp : MonoBehaviour {
public Text countText;
public GameObject myObject;
private int count;
// Use this for initialization
void Start ()
{
count = 0;
countText.text = count.ToString () + "/6" ;
SetCountText ();
myObject.SetActive (false);
}
// Update is called once per frame
void Update () {
if (count == 6)
{
myObject.SetActive (true);
}
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag ("Pick Up"))
{
other.gameObject.SetActive (false);
count = count + 1;
SetCountText ();
}
}
void SetCountText ()
{
countText.text = count.ToString () + "/6" ;
}
}
I simplified the process (because I suck at scripting!) by making the gameObject with the Application.LoadLevel(); be disabled at the Start. So it basically isn’t there at the Start. I duplicated the object at the same spot and removed the script component in it so it looks like it’s there but the player can’t use it UNTIL the player picks up all 6 pickups (hence the count ==6 in the if statement) and when the player does pick up all 6 pickups, the gameObject with the script Application.LoadLevel(); is enabled and the player can then use it.
If anything is unclear for any beginners out there (like me!), please feel free to comment. And again, thanks to all who answered, it’s very much appreciated.