Instaciating a prefab into a list

Hi All,
can anyone explain how to add a prefab into another objects List. In my code when i do so ( code below ) the prefab is cloned but is shown in hierarchy as a separate object. The object inspector shows the list and shows the prefab name as well but it’s not visible? So a new clone object is show in hierarchy, I don’t want this, and the object inspector shows the list and a clone object is added but it’s not shown in the scene/game. It’s almost as if List is a pointer to the created object? How do I add a prefab to the list without adding a separate new clone object to the hierarchy?

/*list of sprites*/
    public List<GameObject> jars = new List<GameObject>( );


    // Use this for initialization
    void Start ()
    {
        jars.Add( Instantiate( GameObject.Find("Jar") ) ); //GameObject.FindGameObjectsWithTag("Jar Sprite") );

    }

Also on a second note the List shows an object clone but I can’t get access to any of its attributes in the inspector, eg it’s x position, how do I show these in the inspector? Thanks

I think you can’t: As far as I know a new GameObject will always be referenced as a seperate object in the hierarchy of the scene it was created in. You could maybe deactivate the new cloned objects and parent them under the object that holds the list, so the new cloned object would at least be kept as children then.

I think the problem here is that the Inspector usually displays the components of a GameObject. If a GameObject itself is referenced as a property within a component, it does not go through all that components of that (Sub-)Gameobject again, but only displays the simple “GameObject reference box” that you are seeing right now in your list. I think it could be possible to override the default inspector behavior to display some / all properties on a referenced GameObject, but I also think it might involve a lot of work / create problems further down the line.

If you don’t want a new GameObject to be created in the hierarchy, would it be a possible workaround to manage your jars with your own “Jar” class first? You then only call “Instantiate” for the jar prefab when the jar is supposed to appear in the game world.

Something like:

[System.Serializable]
public enum JarContent { water,lava,sand}
[System.Serializable]
public class Jar
{
    public JarContent content;
    public int volume = 10;
    public bool inInventory = false;

}


public class MyClass: MonoBehavior{

    public List<Jar> jars = new List<Jar>();

    public void Start()
    {
        jars.Add(new Jar() { content = JarContent.lava, volume = 5, inInventory = false });
        jars.Add(new Jar() { content = JarContent.water, volume = 10, inInventory = true });
}

Should turn out in your inspector like:

So you could add / remove jars to your list, track their properties etc. and only when they really should appear in the gameworld you call Instantiate and maybe “translate” the properties from your Jar-class over to the instantiated prefab.

1 Like