Unity: UI prefabs go invisible when i Instantiate it - c#

I have a custom UI element in the Mainmenu scene that works fine, but when i make a prefab out of it, all the text and images become invisible, and the rect transform goes blank
The orginal prefab
the instatiated invisible prefab

You should instantiate it as a child object of Canvas.
Instantiate(prefab, thatCanvas.transform);

Related

Text on Sprites in Unity

I'm trying to make an application that has sprites going down the screen with numbers in them. The Text(Script) component only works for 3D objects. I've tried using Text Mesh and making that a child of the sprites but I need them to be instantiated as a Prefab. So when I load the sprite in as a prefab it loses it's relationship to the Text Mesh. Does anyone know of a solution that worked for them?
There are many ways to do it depending upon your requirement. I will show you two of the most basic.
1- Use 2D Sprite and TextMesh.
2- Use Canvas Image and Text Objects.
P.S First one, that is 2D Sprite can be Instantiated directly in the hierarchy but for 2nd(Canvas Image) you need to instantiate it inside the canvas obj means as a child of canvas object.

Put animated prefab as image in button

I created an animated prefab and want to use it as image in button. Is this possible? If not what can I do to insert an animated image in button.
I assume you are talking about showing an animated 3D prefab on top of your button.
If you have a 3D prefab and want it to be rendered on top of a button, you need to setup a separate render chain.
In essence you add a camera, that renders your object to a RenderTexture. Then you use this RenderTexture as source for an Image component on top of your button.
The critical part is setting up the camera and prefab, so that it doesn't render the whole scene, and isn't visible from whithin the scene.
There are many ways to do that, but I think the easiest way is to place the prefab and camera far away from the rest so that they are outside the view range of the main camera.
Just make sure the camera sees the prefab (and only the prefab) and has the render texture setup as render target.

Moving Unity GameObject from Scene to Another without moving the UI Canvas or the UI Buttons

i have tried different many ways to move Gameobject without moving the UI-Buttons to another scene, but it always moves the scene with it's UI
Butoons Are Created and givin there handlers inside OnGUI() Event
i used :
SceneManager.LoadSceneAsync(targetSceneName, LoadSceneMode.Additive);
i also used :
SceneManager.MoveGameObjectToScene(element, targetScene);
but it still moving The UI Buttons
is there a way to move GameObjects without UI Components ?
It seems your Canvas is attached to the GameObject your moving. You could destroy the canvas object or canvas components upon import.
void DestroyGameObject()
{
Destroy(gameObject);
}
OR
void DestroyComponent()
{
// Removes the rigidbody from the game object
Destroy(GetComponent<Rigidbody>());
}
You could also try attach your canvas to another object in your initial scene so it isn't attached on import. Hope this helps

How to use UI.Text as a prefab in Unity?

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.
When I drag the prefab, it does not show anything on the scene. This happens to all my prefabs. The following are its components:
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)
}
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);
For each instance, you might have to manually enable the guiTexture.

Gameobject instantiated in canvas shows up behind other canvas items

In my game, if it is the first time the player has ever played the game then I have an image that is displayed. I instantiate it in the canvas like so:
if (PlayerPrefs.GetInt("First Time", 1) == 1)
{
introEnabled = true;
//pause all activity
pause.pauseOrResume();
intro = Instantiate (Resources.Load ("intro"), transform.position, Quaternion.identity) as GameObject;
Canvas canvas = GameObject.Find("Canvas").GetComponent<Canvas>();
intro.transform.SetParent(canvas.transform, false);
//robot = Instantiate (Resources.Load ("robot"), transform.position, Quaternion.identity) as GameObject;
PlayerPrefs.SetInt("First Time", 0);
PlayerPrefs.Save();
}
I can see in the inspector when the game is running that when the object is created, it does show up in the canvas. I just have no idea why it won't show up on top of all of the other objects in the canvas.
The canvas rendering mode is set to "Screen space - Overlay" and it is set to 0 in the sort order. The game object being instantiated also has a sort order of 0 and it is in the default layer.
Nothing I have tried has worked. The z values make no difference and even putting the canvas and the instantiated object in the Default layer and setting the canvas to a sort order of 2 and the instantiated object to a sort order of 1 (so it is rendered first) does not make a difference.
What you want is to use transform.SetAsLastSibling() . The CanvasRenderer renders the GameObjects in order according to their sibling index's (position in the hierarchy), from top to bottom.
However, after examining your code further I noticed something and I should say that if you are trying to instantiate the intro screen, I would highly recommend to instantiate it with it's own Canvas, instead of parenting it to the existing one. Something that significant should have it's own dedicated Canvas.
Update: The problem here is that you are instantiating GameObjects with a transform and Sprite renderer. Canvas UI gets rendered last, over all mesh layers, so if you want your newly instantiated GameObjects to participate, they will need to have RectTransform and Image components instead.
Instead of Default layer change its layer to UI.
You code seems fine, it might be problem related to the gameobject where this script is attached (because you are passing transform.position in Instantiate). it might be positioning the Instantiated game object off the canvas. Try like this once it might work.
intro = Instantiate (Resources.Load ("intro")) as GameObject;
and after setting its parent(very important), set it's position like this
intro.transform.localPosition = new vector3(0,0,0);

Categories

Resources