- Home /
Get method in script with Generic list
Hi guys! Right now I'm developing a game with the ability to instantiate objects on the terrain. Now I'm closing up on 20 different buildings, which right now are all instantiated from one script with a variable for every object which is the one to instantiate, rotate and so on. Now, I would like to clean this up a bit, and my idea was to have every script and the associated building (object) in an empty GameObject, and then have a Generic list containing all scripts and then if I choose for example object 5 it should use the script object-5.cs and and then instantiate the object associated with the object 5 script. How would I do this, or is it another way?
Thanks!
Answer by Azrapse · Oct 20, 2013 at 02:37 PM
You can use either inheritance or delegates.
Inheritance:
Create a c# file with this contents:
public interface IObjectSpawner
{
void DoWork();
}
Then go to all your scripts that generate stuff and add that interface to their inheritance list. For example, if you have a script called TreeGenerator, edit the declaration so that it looks like:
public class TreeGenerator: MonoBehaviour, IObjectSpawner
{
[...] // your script contents
public void DoWork()
{
//Call here to the method in this script that does the spawning.
}
}
And finally, on the centralized place where you want to have a generic list with all the scripts that spawn objects, you can declare it like this:
public List<IObjectSpawner> spawnerList;
And you can iterate over it and call the DoWork() method to spawn their object, for example:
foreach(var spawner in spawnerList)
{
spawner.DoWork();
}
Delegates:
Declare your list as
public List<Action> spawnerMethods;
Add calls to the spawning methods into that list like:
spawnerMethods.Add(myScript1Object.DoSomeWork);
spawnerMethods.Add(myScript2Thingie.Spawn);
spawnerMethods.Add(myScript3Stuff.MakeClone);
Where the methods DoSomeWork, Spawn and MakeClone belonging to those three scripts all have in common that are called like
public void MethodName ()
that is, with no parameters and no return value.
Thanks for your answer, I'm checking it out right now. Do I have to include the raycast and everything? And how do I get for example the SpawnWindmill script, if I wanted to use, let's say $$anonymous$$eycode.O? With Inheritance.
It depends on your particular design. I have no idea what you mean with the raycast. $$anonymous$$aybe you want to cast a ray towards the terrain, then on the point it touches the terrain you want to spawn an object depending on which key the user presses?
Yes, I have a raycast method which is raycasting towards the terrain, but I have a variable called chosenObject which contains an integer, for example 5. So if I, for example, press 5 it changes the chosenObject and therefore changes the object being instantiated. So if I press 5, the list item #5 (for now) should get picked.
Nice. Then create each of those 10 different spawners and add them to the list. On Input, you could use a switch or something to select an index from the list. I would probably create a Dictionary where the key is a $$anonymous$$eycode value and the values are the matching index in the List.
Something like:
protected Dictionary<$$anonymous$$eycode, int> key$$anonymous$$appings = new Dictionary<$$anonymous$$eycode, int>
{
{$$anonymous$$eyCode.Q, 0},
{$$anonymous$$eyCode.W, 1},
{$$anonymous$$eyCode.E, 2}
[...]
}
And then, in the Update method something like:
foreach(var key$$anonymous$$ap in key$$anonymous$$appings)
{
if(Input.Get$$anonymous$$eyDown(key$$anonymous$$ap.$$anonymous$$ey)
{
spawnerList[key$$anonymous$$ap.Value].DoWork();
}
}
Sounds all good to me, I'm gonna try this out tomorrow, it's kind of late over here. Thanks!
Your answer

Follow this Question
Related Questions
A node in a childnode? 1 Answer
Converting Builtin Array to Generic List 2 Answers
How to find index of a list? 3 Answers
Checking if object intersects? 1 Answer
How do we instantiate random sprites from an array... 1 Answer