3

I've tried using this

Instantiate(entry, new Vector3(x, y, 0), Quaternion.identity);

It successfully created objects, as seen in my hierarchy. But I cannot see the text on the screen, even though there is a text assigned to it. I can only see an empty game object on the screen.

These screenshots show the game on play, and the selected object is the object instantiated through the script.

enter image description here

enter image description here

When I drag the prefab, it does not show anything on the scene. This happens to all my prefabs. The following are its components:

enter image description here

JeanLuc
  • 4,783
  • 1
  • 33
  • 47
jeanl
  • 379
  • 2
  • 7
  • 18

3 Answers3

3

In new Unity3D UI system, every UI (Text, Button, Panel etc.) component that will be renderer on screen must have a parent Canvas component. You actually do use such approach in your project, where you have Toolbar, List etc. that have parent called Canvas and which I suppose has Canvas component attached.

What you need to do is to move instantiated gameObject to be child of other gameObject that holds Canvas component. As there can be multiple components of Canvas type, I would suggest to add possibility to assign root for instantiated objects inside your Script.

Script:

//Drag and drop this from inspector; make sure that dragged gameObject
//has Canvas component in it or above it (in parent)
public Transform EntriesRoot;

void Start()
{
    var entry = Instantiate(entry, new Vector3(x, y, 0), Quaternion.identity);
    //Put parent to our EntriesRoot
    //EntriesRoot will be our new parent for entry in hierarchy
    entry.transform.SetParent(EntriesRoot)
}
mwilczynski
  • 3,062
  • 1
  • 17
  • 27
  • 1
    SetParent is not supported in the latest version of Unity which is 5.3. Is there another way to instantiate this prefab as a child of the Canvas object? – jeanl Jan 17 '16 at 07:13
  • I'm using Unity 5.3.1 and it's definitely supported. You should indeed use SetParent on transform component, my bad (edited already). The way that is deprecated now is `transform.parent = otherTransform` not `transform.SetParent(otherTransform)`. What is the exact error? – mwilczynski Jan 17 '16 at 12:52
  • There is a convenience to set the parent transform as part of `Instantiate` as well. `GameObject text = Instantiate(entry, new Vector3(x, y, 0), Quaternion.identity, EntriesRoot);` – Procrastin8 Apr 17 '19 at 23:23
2

they need to be a child of a Canvas, that displays them.

 // find canvas
GameObject canvas = GameObject.Find("Canvas");
// clone your prefab
GameObject text = Instantiate(entry, new Vector3(x, y, 0), Quaternion.identity);
// set canvas as parent to the text
text.transform.SetParent(canvas.transform);
JeanLuc
  • 4,783
  • 1
  • 33
  • 47
0

For each instance, you might have to manually enable the guiTexture.

AcFreeman
  • 316
  • 1
  • 2
  • 8
  • how do i do that? the method Instantiate returns an UnityEngine.Object. That class does not have a field "enabled". – jeanl Jan 02 '16 at 11:27
  • I cant manipulate the object as a text because its type is not GUIText, rather it's Object. Casting doesnt work either – jeanl Jan 02 '16 at 13:07
  • if the prefab is enabled the cloned copy will be enabled too. – JeanLuc Jan 02 '16 at 13:43