- Home /
How can I call a function in all instances of a script
I have a script that instantiates objects. I want to be able to send information to all of these objects, every time the player uses an ability. There is a maximum of 100 objects. What is the most efficient way to do this?
If all of these objects share a commons script (say Enemy.cs), then using the message passing system isn't the best way to do this.
You'd rather have the logic within Enemy.cs, wherein you check for Player.ability (lets say this is a singleton that doesn't inherit from $$anonymous$$onobehavior) in Update
Answer by sprawww · Mar 23, 2015 at 08:45 PM
Since you are instantiating the objects yourself, you can use the same class to keep track of all the created objects. What exactly it is that you use to keep track of the objects is up to you. Personally, I like List. Once you have that, you can just loop through it to send the information to all the objects.
using System.Collections.Generic;
List<GameObject> goList = new List<GameObject>();
void InstantiateAddList()
{
GameObject go = Instantiate(//...);
goList.Add(go);
}
void OnAbilityUse()
{
//Ability use stuff
foreach(GameObject go in goList)
{
go.SendInformation();
}
}
Okay I have this code.
GameObject obj = Instantiate(projectile,new Vector3((x - 4) * 0.6F,5,-9),Quaternion.identity) as GameObject;
orbs.Add(obj.GetComponent(typeof(orb)));
That gives an error saying: "NullReferenceException: Object reference not set to an instance of an object Deck+c__Iterator1.$$anonymous$$oveNext () (at Assets/Scripts/Deck.cs:160) UnityEngine.$$anonymous$$onoBehaviour:StartCoroutine(String) Deck:save() (at Assets/Scripts/Deck.cs:79) UnityEngine.EventSystems.EventSystem:Update() ."
Does the code work fine without the new
orbs.Add(obj.GetComponent(typeof(orb)));
? Please remove or comment that line to make sure it is the one throwing the error.
Afterwards, you might want to replace that with
orbs.Add(obj.GetComponent<orb>());
we can go from there if the error still exists
It gives the error as the code executes. So no the code needs the line to work properly.
Edit: Actually yes the code works fine without it.
I don't see any error in it, there could be something further up. Can you remove or comment the .Add line and add these ins$$anonymous$$d
Debug.Log("1: " + orbs);
Debug.Log("2: " + obj);
Then tell me the output Unity gives you. Least you'll know which of the 2 is causing the problem.
It appears that obj is giving the error. Could it be something to do with Instantiate being casted as a GameObject?
Answer by Cherno · Mar 23, 2015 at 08:49 PM
sprawww is right, but I'm gonna directly answer the original question anyway. It's definitely NOT efficient, though. Keeping track of all gameobjects/scripts manually is the best way I figure.
YourScriptName[] yourScriptArray = FindObjectsOfType(typeof(YourScriptName)) as YourScriptName[];
foreach (YourScriptName yourScriptName in yourScriptArray ) {
yourScriptName .FunctionName();
}
And if you also need to find the inactive objects:
Resources.FindObjectsOfTypeAll<YourScriptName>()
This will likely be even slower than FindObjectsOfType.
Thank you!!! I've been trying to get my script to work all day, and this solved my problem. Awesome!!
Answer by DiegoSLTS · Mar 23, 2015 at 08:55 PM
If you can make all the instantiated objects childs of the same game object you can use the BroadcastMessage function: https://docs.unity3d.com/ScriptReference/Component.BroadcastMessage.html
It will call the function with the name you want in every component that has that function, for the game object that has the script calling the function and all it's children. I haven't tested it, but I guess it goes through the full hierarchy of childs, not just the first level.
I don't know if this is efficient, probably it takes more time than having a list of objects to call that function, but maybe you can trade a little speed at runtime for more speed at coding.