Good morning. I have a related question here GameObject properties collection can be added to? but this is an attempt to get help by making the problem more specific and less general.
I have a scene with hundreds of GameObjects, a few dozen of which have scripts attached with properties that affect its behavior in game.
At certain points of the game, I’d like to reset these few dozen objects to their original states. To make it more concrete let’s state that the few dozen objects each are either a Door, Window, Ball, or Chair, each of which have prefabs made with scripts attached to them holding these properties.
What I’d like is a variable array set up in the editor which the game’s designer can fill with objects that need this rest ability. Armed with this array of ResettableGameObjects in my GameManager, when the time comes to reset the GameObjects, I’d like to call a method of the GameObjects in a loop such as:
for (int i = 0; i <= ResettableGameObjects.Length - 1; i++)
{
ResettableGameObjects*.*Reset()
}
Unfortunately, you cannot extend the GameObject to add such a method.
I could add a Component script to each prefab that contains a Reset function, however as each type of game object(Door, Window, Ball, Chair) has different things that needs reset I cannot use a generic component script named “Reset.cs” unless I want to make the costly calls of finding out which type of object I am attached to. So, I cannot seem to make a generic loop as shown above. I’d need differently named component scripts for each type of object.
Is there a simply way to achieve the type of reset loop I am looking for above in which a “GameObject’s” reset function is called without needing to know exactly what type of GameObject we have?