Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm creating what is essentially my own prefab system. Game objects are defined by a human readable data file. I'd like to create an empty GameObject, load it with the components defined in the data file and have it ready and waiting for an Instantiate() call. However, whenever I use GameObject go = new GameObject() a new game object is added to the scene.

Using Unity built in prefabs, I can load a prefab as a GameObject, and it's not added to the scene. (See example below)

The rationale behind this is I'd like to have one list that contains GameObjects, some generated by loading Unity prefabs and others created by my custom prefab system. This needs to happen at run time, and can't involve the Unity Editor (since ideally end users would be able to define their own data files).

How can I create a new GameObject, without Unity automatically instantiating it for me?


For example, I can make a call like this:

Resources.LoadAll("PrefabsDirectory", typeof(GameObject))

And I'll get a list of GameObjects, none of which are added to the scene when the call is made. Essentially, I'm creating a LoadAll method for my own prefabs, and likewise, I don't want to instantiate the GameObjects when the call is made.

share|improve this question
1  
I'm afraid you can't. I think you'll have to delay the instantiation of your class. –  Roberto Mar 11 '14 at 1:04
    
It's been a while since I touched Unity, but if it turns out there isn't a way around this, could you just remove the object from the scene immediately after and have the desired outcome, just slightly inefficiently? –  Kevin Reid Mar 11 '14 at 1:39
    
@KevinReid I believe the only way to do that is call Destroy on the GameObject. Which removes it from the scene and releases it, making it unusable. –  Byte56 Mar 11 '14 at 1:58
1  
@PandaPajama has a good idea. Calling go.SetActive(false) is the best way to create something like an object pool. It's not exactly what you want, but perhaps you could have a small group of inactive objects that are later cloned for your custom instantiation. –  AndrewEkeren Mar 11 '14 at 5:36
1  
@Byte56: Why exactly do you want to pre-instantiate your custom prefabs and not just compose the GO with required components on the fly while instantiating them using a factory like pattern? For what I understood you want to allow users to "serialize" prefabs using "their own data files", so you have to read those files at run time anyway and instantiate the required components.. am I missing something? –  Heisenbug Mar 11 '14 at 13:39

3 Answers 3

up vote 2 down vote accepted

You could change the GameObject's hideFlags?

I just tried running this:

GameObject myGameObject = new GameObject ();
myGameObject.name = "Obvious Name";

myGameObject.hideFlags = HideFlags.HideInHierarchy;

...and it's there, but it's not in the hierarchy. Technically, it's in the scene, but you don't see it until you change it's flags back to HideFlags.None. It's not the answer you're looking for, but at least as far as releasing this to developers goes, it's cleaner than grouping your objects.

share|improve this answer
1  
This, paired with disabling it (so none of the scripts execute), could work. I'll give it a try and let you know. It's still a work around, but I think that's what I'm left with at this point. And, for all we know, this could be what's done internally, though I doubt it. –  Byte56 Apr 14 '14 at 1:10
1  
@Byte56: I tought it was a workaround too, but I actually I discovered these days that Unity does the exactly same thing when previewing animations: it instantiate hidden gameobject in the scene and render them to the preview frame with a dedicated camera. So maybe it doesn't sound so clean, but actually this solution seems to be used also internally. –  Heisenbug Apr 15 '14 at 16:21
1  
Sorry for the late accept. I just got around to implementing this and it works great. Thanks. –  Byte56 Apr 26 '14 at 4:30

Basically there is no way to do go = new GameObject; without adding it to the scene.

I'm working on an rts where player base needs to be persistent, essentially we serialize everything they've done and load it back in at run time using modified assets from the store.

Most output to xml or json so its really pretty straight forward to load the file and manipulate it as you wish.

Hope this helps

Marc

share|improve this answer
    
Thanks Marc. Can you provide evidence or rationale for not being able to create a new GameObject without adding it to the scene? I have serialization code of my own, so that's not a problem, thanks for the suggestion. –  Byte56 Mar 22 '14 at 14:54

You can't do this exact thing unfortunately. However you can accomplish the same design pattern (ie. abstract away the building of your objects, and then simply call them with a single command later) through a Factory pattern. When you want an object, call something like Factory.CreateGameEntity() and that method will handle everything behind the scenes.

ADDITION: aha just read through the comments, someone else had brought this up. I don't understand your explanation for why you don't want to do this, it seems like a very clean design to me.

share|improve this answer
    
It's more complex than you might think though, making it not as clean. I'll post an answer will full details, but essentially the parser has to be pretty complex. For example, if I want to test if a prefab has a component, before instantiating, it's as simple as GetComponent<>() != null. With my data files, the parser has to search for the correct component in the data. Same for getting a value from inside a component, and so on. The GameObject class provides a lot of functionality that needs to be duplicated by the parser. –  Byte56 Mar 22 '14 at 14:51
    
If you can provide evidence or even some rationale for why this can't be done, it would make this answer complete. –  Byte56 Mar 22 '14 at 14:52
    
I'll see later if I can find any definitive documentation about this, for now all I can find are these forum threads forum.unity3d.com/threads/… - answers.unity3d.com/questions/366273/… - answers.unity3d.com/questions/19736/… –  jhocking Mar 22 '14 at 16:06

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.