Add gameobject dynamically to scene in Unity3d

61,279

Solution 1

//Drag object prefab to variable in inspector
public GameObject spawnObject;
//----------------------------------------

Below will create GameObject using the objects Own Transform settings.

 GameObject clone;
    clone = Instantiate(spawnObject.transform, 
                        spawnObject.transform.position, 
                        spawnObject.transform.rotation) as GameObject;

Below will create GameObject using the objects Parents Transform settings.

 GameObject clone;
    clone = Instantiate(spawnObject.transform, 
                        transform.position, 
                        transform.rotation) as GameObject;

Not sure if this helps, but good luck on your game :)

Solution 2

In Unity, you can do it like this.

 GameObject.Instantiate(prefab,new Vector3(1,1,0),Quaternion.identity);

See also : http://docs.unity3d.com/Documentation/ScriptReference/Object.Instantiate.html

Especially for Position, it must in front of your Camera, or you may not see it.

What's more, I suggest you take a look to NGUI. It's a powerful GUI system with some useful API for develop. BTW I can't imagine how hard it is to develop games without such thing, so you may need it sooner or later ;\

With it, you can do so easily.

Gameobject go = NGUITools.AddChild(Gameobject Parent, Gameobject Prefab)

UPDATE:

When I answered this, NGUI is the ONLY usable gui system, so I recommended it. However, there's an official Unity UI system(AKA uGUI) out there, you don't really have to use NGUI, leave alone the gui war is still continuing.

What's more, you may want to take a took into pool system. It's used to handle massive gameobjects like bullets, cubes etc. If you have hundreds of specific gameobject in the same scene and suffering from instantiate, then you probably need a pool. Personally I tried FastPool and it works good, actually all assets of its kind work exactly the same.

Share:
61,279
Ashwani K
Author by

Ashwani K

Full-stack developer with experience in developing Spring, AWS, Android, java, nodejs, and many more applications.

Updated on July 09, 2022

Comments

  • Ashwani K
    Ashwani K almost 2 years

    I am creating a scene in which I want to show list of offers. In order to show the offer, I created a prefab with placeholders for the offer details which I will get at runtime. I created a place holder in the scene to add the prefab to the scene, but it is not showing on the UI. OfferHolderClass:

    using UnityEngine;
    using System.Collections;
    
    public class OfferHolder : MonoBehaviour {
    
        public GameObject localOffer;
        // Use this for initialization
        void Start () {
            GameObject offer = Instantiate(localOffer) as GameObject;
            offer.GetComponent<Offer>().Text = "Testing";
            offer.transform.parent = this.transform;
        }
    
        // Update is called once per frame
        void Update () {
    
        }
    }
    

    I am new to Unity and am not sure what I am missing here.