Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Problem with prefabs with same script are all triggering a coroutine at the same time

Discussion in 'Scripting' started by drgareeyg, May 16, 2022.

  1. drgareeyg

    drgareeyg

    Joined:
    Jan 20, 2020
    Posts:
    3
    Hey all, currently I'm trying to code a situation where if the player steps within the trigger collider of a tree, and presses "F", it would start a coroutine to chop the tree.

    Right now, it starts this coroutine in ALL tree objects that are created from this prefab, which is not how I want it; I only want the tree that the player is standing in the trigger of to be deleted.

    I've been stuck on this for a few hours and I can't seem to figure it out and I've exhausted google searches... I'm sure the answer is obvious but I'm a newbie :(


    Code (CSharp):
    1.  
    2.  
    3. public class TreeScript : MonoBehavior
    4. {
    5.     [SerializeField] private int treeHealth = 5;
    6.     bool isBeingChopped = false;
    7.     bool inTreeTrigger = false;
    8.  
    9.  
    10. void Update()
    11.     {
    12.         if (inTreeTrigger = true && Input.GetKeyDown(KeyCode.F))
    13.         {
    14.             StartCoroutine(CoChopTree());
    15. }
    16.  
    17. void OnTriggerEnter(Collider other)
    18.     {
    19.  
    20.         if (other.tag == "Player")
    21.         {
    22.             inTreeTrigger = true;
    23. }
    24. }
    25.  
    26. void OnTriggerExit(Collider other)
    27.     {
    28.         if (other.tag == "Player")
    29.         {
    30.             inTreeTrigger = false;
    31.         }
    32.     }
    33.  
    34.  
    35. private IEnumerator CoChopTree()
    36.     {
    37.  
    38.         isBeingChopped = true;
    39.         WaitForSeconds waitTime = new WaitForSeconds(1f);
    40.  
    41.         while (treeHealth > 0 && isBeingChopped == true)
    42.         {
    43.             treeHealth = treeHealth - 1;
    44.             Debug.Log(treeHealth);
    45.             yield return waitTime;
    46.         }
    47.    
    48.         isBeingChopped = false;
    49.         Debug.Log("Stopping");
    50.         Destroy(this.gameObject, 1f);
    51.     }
    52.  
    53.  
     
    Last edited: May 16, 2022
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,003
    Can you show us the entire script?
     
  3. drgareeyg

    drgareeyg

    Joined:
    Jan 20, 2020
    Posts:
    3
    I've updated it to show the entire script, thank you for taking a look.
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    Your problem is here:
    if (inTreeTrigger = true

    You use
    ==
    to compare, not
    =
    . The way you have it now you are setting that to true every frame in every copy of the script.

    Better yet you don't need that comparison at all, since
    == true
    is never necessary. You can just write that line simply as
    if (inTreeTrigger && Input.GetKeyDown(KeyCode.F))
     
  5. drgareeyg

    drgareeyg

    Joined:
    Jan 20, 2020
    Posts:
    3
    I'm an idiot. Thank you.