22

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.

Alex Cio
  • 6,014
  • 5
  • 44
  • 74
Ashwani K
  • 7,880
  • 19
  • 63
  • 102
  • Have you made sure you instantiate the prefab at the correct location? try specifying the coordinates manually. – Botz3000 Mar 19 '13 at 13:00
  • What @Botz3000 said. When you call Instantiate w/o position or orientation arguments those values are taken from your prefab's defaults. – Jerdak Mar 19 '13 at 14:25
  • What coordinates can I give? – Ashwani K Mar 19 '13 at 16:34
  • You can add a `Debug.Break ()` Statement at the end of `Start ()` and then look if something like `Offer (Clone)` shows up in the hierachy view. – Kay Mar 19 '13 at 16:35
  • @AshwaniK Give it Instantiate(localOffer,Vector3.zero,Quaternion.identity) for an object placed at origin. Alternatively just look at the Unity editor hierarchy(in preview mode), if your object was instantiated it'll be in the list and you can double click it to see if it's actually in the scene and it's just that the camera is not facing the right way. – Jerdak Mar 19 '13 at 16:39
  • 1
    Did you actually drag and drop the prefab onto the script variable in Inspector tab? – 0xC0DED00D Mar 22 '13 at 08:57

2 Answers2

16
//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 :)

UnityFan2013
  • 176
  • 1
  • 3
8

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.

zhuchun
  • 193
  • 4
  • 13