- Home /
Instantiate new object from scratch
Hello,
is there a way to Instantiate a new prefab object without dragging the prefab to public variable or having an existing copy of the prefab in the scene (and use "findGameObjectWithTag" )?
basically i'm trying to fill a List array with some prefabs but the list of prefabs can be changed. so i don't want to drag all of them to variables.
thanks!
Not sure what you are going for here. You can create a new game object:
var go = new GameObject();
This creates an empty game object. You can add the 'go' to a list or whatever you need. You can also build it up by adding components. Note if you want it to exist at edit time, you will need to fill your array using an editor script.
not sure this will help me unless the "new GameObject" can be an existing prefab...
this is my scenario: i have some prefabs in my game. i need to add some of them to List Array in specific order for example:
List.add = prefab1;
List.add = prefab2;
List.add = prefab3;
to do that i need to do one of two things:
assign the prefab to public gameobject prefab1 ,prefab2,prefab3...etc
if the prefab already exist in scene i can find it by tag and add to the list (but they not exist in the scene so i can't use this)
the prefabs and order may be changed from level to level. later i want to instantiate these prefab from the List Array by the order they appear.
so, is there another way to do this?
I missed the boat on what you were trying to do, so I've converted my answer to a comment. I thought you were just looking for some placeholder objects, and/or dynamically build a game object. @Jamora's answer will allow you to dynamically fill your list.
Answer by Jamora · Aug 15, 2013 at 07:10 AM
You can have your prefabs in a Resources folder. Everything inside a Resources folder will be added to the build - regardless of actually being used - so only add necessary objects.
You can load resources from a Resources folder with Resources.Load during runtime.