Pickup number of objects and have another object appear?
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" ;
}
}
why you do that if you just want to load level :
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag ("Pick Up"))
{
other.gameObject.SetActive (false);
count = count + 1;
SetCountText ();
if(count == 6)
// load level
}
}
unless you have other reasons
I don't want the player to have the ability to load the next level without picking up ALL the pickup objects FIRST. So I want the player to pickup 6 objects in total and then the player can go to the next level via Application.LoadLevel();. I don't want the player to pickup (for example) 5 objects and just move on to the next level, that's why I want to be able to enable and disable the script.
but you have already added count = count + 1;
which will increase the count
variable by 1 each time picking one pickup object so logically if the count = 6 that means you have packed 6 objects correct me if i'm wrong
Answer by Wach3D · Apr 20, 2016 at 06:15 AM
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.
Answer by M-Hanssen · Apr 18, 2016 at 01:07 PM
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";
}
Sadly, it didn't work. I was able to pick up less than 6 pickup objects and go straight to my gameObject with`Application.LoadLevel();` and go to the next scene without needing to pick up all 6 pickups.
Is there a way to disable the script on the gameObject property at Start and have it enabled when the player picks up all 6 pickups?