- Home /
Instantiating an object only once within Update()
This is what i have so far. if i collect some coins they get stored in the GameBrain script. when i make it to a chest i can store the coins in there and that value gets stored in the GameBrain at coinsStored. i have that all working fine i even had the object instantiating after i stored 10 coins. its when i tried to make it happen only once by adding the if(UnlockComplete = false)statement. (i know there is nested loops i have fixed that but still no change.) that i have issues with it not spawning at all.
public class UnlockFloor : MonoBehaviour {
public GameObject obj; //passed from unity
public float UnlockAmout = 10f; //usually set by unity 10 is default
private bool UnlockComplete = false; //Stops a MillionBajillion "Things" from spawning
void OnDrawGizmosSelected() { //A Gizmo so i can see the "Things" spawner
Gizmos.color = new Color(1, 0, 0, 0.5F);
Gizmos.DrawCube(transform.position, new Vector3(1, 1, 1));
}
void Update () {
if(GameBrain.coinsStored > UnlockAmout) {
if(UnlockComplete = false){
UnlockTheThing();
UnlockComplete = true;
}
}
}
//Unlocks the "Thing" in question set by obj
void UnlockTheThing(){
Instantiate(obj, transform.position, Quaternion.identity);
}
}
Answer by _Toxic · Oct 15, 2015 at 08:44 AM
For googlers coming here in the future 1st of all greetings from the past. 2nd here is what went wrong
if(UnlockComplete = false){
should read
if(UnlockComplete == false){
= needs to be ==
hahahahaha hello from the future! this was WAY different than I was looking for but I liked your greeting.. so ya know. XD