- Home /
Calling a function from all instances of a script
Firstly, I apologize if the answer here is obvious - the more experience I get with scripting, the worse I seem to get at it. I know this is actually just that I'm getting better and realizing what I don't know, but I apologize anyway.
As my game runs, a number of copies of the same prefab are instantiated. These are destroyed in 3 ways:
-The player clicks on the object
-The player pushes a special button that destroys all of the objects
script attached to the object destroys the object at the end of it's life
Sounds simple, right? Well, the game object instantiates another game object (the "death effect") right before it it destroyed. Clearly I can't put this instantiate code into the "OnDestroy" function because any objects instantiated from "OnDestroy" remain in the editor after I've exited play mode.
So, what I've done is make a function called "DestroyThisObject". I use the "OnMouseDown" function to call this function when the collider for the object receives input, which works fine. However, I can't figure out how to call "DestroyThisObject" from my button object, for all instances of the object being destroyed.
To make this all more tangible, some code (modified with generic names to make it more readable, hopefully):
ObjectA, the prefab, is tagged with tag "Tag1" has the following script attached to it, called "DestroyOnClick":
#pragma strict
function OnMouseDown ()
{
if (Time.timeScale != 0) // used to control input while paused
{
DestroyThisObject ();
Destroy (gameObject);
}
}
public function DestroyThisObject ()
{
//Code for instantiating the death effect
//Code for adding 1 to the score
}
So, ObjectB needs to call the "DestroyThisObject" function on ALL instances of this object. What I have right now looks like this:
#pragma strict
private var otherGameObjects: GameObject;
private var destroyOnClick : DestroyOnClick;
function OnMouseDown ()
{
otherGameObjects = GameObject.FindWithTag ("Tag1");
destroyOnClick = otherGameObjects.GetComponent (DestroyOnClick);
destroyOnClick.DestroyThisObject ();
}
What this does is locates the first object with Tag1, and runs the "DestroyThisObject" function on it, and it works beautifully. However, I can't think of a way to get it to run the function on every single instantiated prefab. I can make an array of all of the game objects with Tag1 and destroy them, but that doesn't run the "DestroyThisObject" function so it doesn't really work. If I could make some sort of static function and just call "DestroyOnClick.DestroyThisObject", that would be great, but if I attempt that, I get a lot of errors that say "An instance of type 'DestroyOnClick' is required to access non-static member ... ".
Many thanks!
Answer by Grimrender · Apr 24, 2014 at 05:38 AM
Hello everyone.
I'm not sure if it is really considered cool to answer my own questions, I think this'll be the second time I've done it. However, I'd rather an answer be out there in case someone comes across this question with a similar problem.
I still have no idea how to call a function as I originally intended to. However, to obtain the behavior I was after, which is the important part, is really quite easy: Simply place a static boolean variable in the same script as the function you want to call. In the update function of that script, check for the status of the boolean and run the function if the boolean is true. Change the variable from whatever other script you're calling it from when you want the behavior to take place. This method works great for my purposes - I don't like that it uses the update function, but it is, at least, just a very simple if statement, so it shouldn't be too expensive.