Search Unity

Trying to set up a planting and harvesting system, but it only works in one trigger area?

Discussion in 'Scripting' started by unity_G_fkbsSNWK3YcQ, Feb 17, 2019.

  1. unity_G_fkbsSNWK3YcQ

    unity_G_fkbsSNWK3YcQ

    Joined:
    Dec 30, 2018
    Posts:
    12
    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?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PowerFarmCoroutine : MonoBehaviour {
    6.  
    7.     public bool instantiated;
    8.     public bool inside;
    9.     public bool collected;
    10.     private IEnumerator coroutine;
    11.     public GameObject powerorb;
    12.     public Material[] material;
    13.     Renderer rend;
    14.  
    15.     void OnTriggerStay2D(Collider2D Collider2D)
    16.     {
    17.         if (Collider2D.gameObject.tag == "Player")
    18.         {
    19.             if (instantiated == false)
    20.             {
    21.                 inside = true;
    22.             }
    23.  
    24.             if (collected == true)
    25.             {
    26.                 instantiated = false;
    27.                 collected = false;
    28.                 rend.sharedMaterial = material[0];
    29.             }
    30.         }
    31.     }
    32.  
    33.     void OnTriggerExit2D(Collider2D Collider2D)
    34.     {
    35.         inside = false;
    36.     }
    37.  
    38.  
    39.         // Use this for initialization
    40.     void Start () {
    41.  
    42.         rend = GetComponent<Renderer>();
    43.         rend.enabled = true;
    44.     }
    45.    
    46.     // Update is called once per frame
    47.     void Update () {  
    48.  
    49.         if (inside && Input.GetKeyDown(KeyCode.LeftAlt))
    50.         {
    51.             StartCoroutine("spawning");
    52.         }
    53.  
    54.  
    55.         if (instantiated == true)
    56.         {
    57.             StopCoroutine("spawning");        
    58.         }
    59.     }
    60.  
    61.     IEnumerator spawning()
    62.     {
    63.         rend.sharedMaterial = material[1];
    64.  
    65.         yield return new WaitForSeconds(8);
    66.         Instantiate(powerorb, transform.position, transform.rotation);
    67.         instantiated = true;
    68.         inside = false;
    69.        
    70.         yield return null;
    71.        
    72.     }
    73. }
    74.  

    I reference if the object has been collected in another script with this:

    Code (CSharp):
    1.  if (collecting)
    2.             {
    3.                 GameObject.FindWithTag("PowerFarm").GetComponent<PowerFarmCoroutine>().collected =
    4.             true;
    5.                 Destroy(gameObject);
    6.             }
     
    Last edited: Feb 17, 2019