- Home /
How to instantiate several objects from a single string
what i'm trying to do is instantiate several objects from one string containing many names.
lets say we have
private string nameofobjects;
void Update ()
{
nameofobjects = nameofobjects+"_"+(text you entered);
//Example nameofobjects would be "_car_bus_house"
}
void Start()
{
//Instantiate nameofobjects as gameobjects
}
Answer by Statement · Dec 25, 2013 at 09:46 PM
Use String.Split to get an array of object names.
char[] separator = new char[] {'_'};
StringSplitOptions options = StringSplitOptions.RemoveEmptyEntries;
string[] names = nameofobjects.Split(separator, options);
Then you can do something for each string. I don't know what you want to do, but the most obvious thing that came to mind was loading game objects via Resources.
foreach (string name in names)
Instantiate(Resources.Load(name));
in the for loop i will instantiate an empty gameobject and rename the instantiated object. this should work just fine , thank you =]
Answer by GaRzY · Dec 25, 2013 at 09:51 PM
The nameofobjects string contains class names (ex: Car.cs, Bus.cs) or only you want to do this?:
GameObject obj = new GameObject(); obj.name = "car".
i have actually set an empty gameobject to be instantiated so i'm just going to rename the instantiated gameobject to bus, car... etc in a for loop
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
instantiate prefab as child of gameobject with tag 1 Answer
How to compare and detect overlapping gameObject Prefabs using maths 0 Answers
Multiple Cars not working 1 Answer
Instantiate prefab once help C# 1 Answer