What I want to happen is when the player presses alt, some time passes and an object is instantiated there. I also want it so that the player can't instantiate another object there until they've collecting the one they've planted there already. This is working when I only have one planting area, but once I have more than one planting area it no longer works- for some reason I'm only able to keep planting and collecting in just one of the trigger areas instead of all of them. What am I doing wrong?
public bool instantiated;
public bool inside;
public bool collected;
private IEnumerator coroutine;
public GameObject powerorb;
public Material[] material;
Renderer rend;
void OnTriggerStay2D(Collider2D Collider2D)
{
if (Collider2D.gameObject.tag == "Player")
{
if (instantiated == false)
{
inside = true;
}
if (collected == true)
{
instantiated = false;
collected = false;
rend.sharedMaterial = material[0];
}
}
}
void OnTriggerExit2D(Collider2D Collider2D)
{
inside = false;
}
// Use this for initialization
void Start () {
rend = GetComponent<Renderer>();
rend.enabled = true;
}
// Update is called once per frame
void Update () {
if (inside && Input.GetKeyDown(KeyCode.LeftAlt))
{
StartCoroutine("spawning");
}
if (instantiated == true)
{
StopCoroutine("spawning");
}
}
IEnumerator spawning()
{
rend.sharedMaterial = material[1];
yield return new WaitForSeconds(8);
Instantiate(powerorb, transform.position, transform.rotation);
instantiated = true;
inside = false;
yield return null;
}
I reference if the object has been collected in another script with this:
if (collecting)
{
GameObject.FindWithTag("PowerFarm").GetComponent<PowerFarmCoroutine>().collected =
true;
Destroy(gameObject);
}