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. Dismiss Notice

Resolved Find all same tags and activate function in their script one by one

Discussion in 'Scripting' started by Realspawn1, Oct 3, 2023.

  1. Realspawn1

    Realspawn1

    Joined:
    Jun 8, 2015
    Posts:
    126
    So not all at once. Anyone can give me an example how to accomplish this ?
    thank you for your time.
     
  2. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,593
    Probably use FindGameObjectsWithTag, and then use
    GetComponent<scriptName>()
    to get the actual script on each object in the array, and then just call it.
     
  3. Realspawn1

    Realspawn1

    Joined:
    Jun 8, 2015
    Posts:
    126
    I got that to work allready but then they all do it at the same time. How would it use some kind of order to do it.
    So first one after that the seccond till all objects are done :)

    Code (CSharp):
    1.  public void AllPGoRight()
    2. {
    3.      GameObject[] obstacles = GameObject.FindGameObjectsWithTag("PP01");
    4.      foreach (GameObject obst in obstacles)
    5.          obst.GetComponent<PP01>().Right = true;
    6.  
    7.  
    8.  
    9. }
     
  4. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    510
    If the order isn't important:

    Code (CSharp):
    1.       GameObject[] entities=GameObject.FindGameObjectsWithTag("Entity");
    2.       foreach(GameObject obj in entities)
    3.          obj.GetComponent<EntityScript>().Invoke("Hello",Random.Range(0,5f));
     
  5. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,468
    They don't "do it at the same time". The code above sets "Right" one after the other. Nothing above executes in parallel, it's a single thread.

    You'll need to be specific. What order do you want? Right now it sets them as they appear in the array you've got.
     
    Kurt-Dekker likes this.
  6. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    510
    Code (CSharp):
    1.    void Start()
    2.    {
    3.       StartCoroutine(AllPGoRight());
    4.    }
    5.  
    6.  
    7.    IEnumerator AllPGoRight()
    8.    {
    9.       GameObject[] obstacles = GameObject.FindGameObjectsWithTag("PP01");
    10.       foreach (GameObject obst in obstacles)
    11.       {
    12.          obst.GetComponent<PP01>().Right = true;
    13.          yield return null;
    14.       }
    15.    }
    16.  
     
    MelvMay likes this.
  7. Realspawn1

    Realspawn1

    Joined:
    Jun 8, 2015
    Posts:
    126
    Well what i use now sends the activation to all objects with that name tag. And in game it make all those objects execute the going right function by setting their bool to true for the eye at the same time. I would like to see the first object with tag name go to the right and then after certain seconds the next and so on till they all have been activated and went to the right. I Don't think i can explain any clearer.
     
  8. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,468
  9. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    510
    Code (CSharp):
    1.        void Start()
    2.       {
    3.          StartCoroutine(AllPGoRight());
    4.       }
    5.  
    6.  
    7.       IEnumerator AllPGoRight()
    8.       {
    9.          GameObject[] obstacles = GameObject.FindGameObjectsWithTag("PP01");
    10.          foreach (GameObject obst in obstacles)
    11.          {
    12.             obst.GetComponent<PP01>().Right = true;
    13.             yield return new WaitForSeconds(2);
    14.          }
    15.       }
     
    MelvMay likes this.
  10. Realspawn1

    Realspawn1

    Joined:
    Jun 8, 2015
    Posts:
    126
    That was it very much thank you both for your time :)
     
    MelvMay likes this.