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

[Solved]Calling function from all objects in level with same tag

Discussion in 'Scripting' started by Greencz, Aug 16, 2014.

  1. Greencz

    Greencz

    Joined:
    Mar 29, 2014
    Posts:
    4
    Hi,

    is there a way how to call function from script that is located in many on the level?
    Let's say I wanna call public void Spawn() that is located in Spawner script inside SpawnPoint game object.

    Thanks for any help.
     
  2. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,680
    Use a manager and delegates.


    In C#, you can use a script as a variable, which this script can then access.


    Example...
    Code (csharp):
    1.  
    2. public spawnerScript[] spawners;
    3.  
    4.  
    5. void mngInstructAllSpawners(){
    6. for (int i = 0; i < spawners.Length; i++){
    7. spanwers[i].spawn();
    8. }
    9.  
     
  3. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    Just use FindGameObjectsWithTag to get an array of all objects with the tag, then iterate through the array and do GetComponent<Spawner>().Spawn() on each item.

    --Eric
     
  4. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,680
    @Eric5h5 granted your method is more flexible, if the spawner count varies, but isn't getting the component of each object something to avoid? Just curious your thoughts in hit to the CPU.
     
  5. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    Not really, but if you were going to do this frequently and the GameObjects don't change, you could store the components in an Spawner array and call the Spawn function directly.

    --Eric
     
  6. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,680
    Hm.
    I should look into cost of GetComponent on mobile. It could save me some serious micro management.
     
  7. Greencz

    Greencz

    Joined:
    Mar 29, 2014
    Posts:
    4
    I don't understand how do I get them into that array? o.o
     
  8. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,680
    ///manager script
    contains public variable delegateScript[], delagateScripts.

    Now, any objec with the component, delegateScript, can be added to the array, on the managers script. Simply drag and drop.
     
  9. Greencz

    Greencz

    Joined:
    Mar 29, 2014
    Posts:
    4
    Hmmm I see, but is there a way how to add them by scripting? Since I want this to be bit more flexible. I was thinking about this code


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class StartingChoice : MonoBehaviour {
    5.     SpeedField difficulty;
    6.     Spawner spawner;
    7.     GameObject[] spawnPoints;
    8.     // Use this for initialization
    9.     void Start () {
    10.         difficulty = GameObject.Find ("SpeedField").GetComponent<SpeedField> ();
    11.  
    12.  
    13.     }
    14.     void OnGUI()
    15.     {
    16.         GUI.Label (new Rect (20, 100, 200, 50), "Choose difficulty");
    17.                 if (GUI.Button (new Rect (40, 150, 100, 50), "Easy")) {
    18.                         difficulty.easy = true;
    19.             spawnPoints= GameObject.FindGameObjectsWithTag ("Spawn Detector");
    20.             for(int i=0; i < spawnPoints.Length; i++)
    21.             {
    22.                 spawner = spawnPoints[i].GetComponent<Spawner>();
    23.                 spawner.ReRoll();
    24.                 spawner.CreateBlock();
    25.  
    26.  
    27.             }
    28.             Debug.Log (spawnPoints.Length);
    29.             Destroy (gameObject);
    30.  
    31.         }
    32.     }
    33.     // Update is called once per frame
    34.     void Update () {
    35.  
    36.     }
    37. }
    38.  
    but it doesn't work for some reason.

    Also this is error I am getting
    NullReferenceException: Object reference not set to an instance of an object
    StartingChoice.OnGUI () (at Assets/Scripts/StartingChoice.cs:23)
     
  10. Greencz

    Greencz

    Joined:
    Mar 29, 2014
    Posts:
    4
    Nevermind I had bad tag used, thanks for your help anyway ^_^;