- Home /
text and image prefab not popping up when set.active
Hi guys. I made a script so when a player hit an object a ui window pops up, the scene freezes for 3.5 seconds and then it resumes. Although I managed to run the co routine successfully when the player hits the trigger no UI elements (neither the image nor the text) are popping up. I ve made a prefab for both of them. Any idea why this is happening?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GUIwin : MonoBehaviour {
public GameObject textprefab;
public GameObject imageprefab;
IEnumerator OnTriggerEnter(Collider other)
{
if (other.CompareTag ("Player")) {
textprefab.SetActive(true);
Time.timeScale = 0.0f;
yield return new WaitForSecondsRealtime(3.5f);
textprefab.SetActive(false);
Time.timeScale = 1.0f;
}
}
}
Answer by HarshadK · Jul 10, 2017 at 01:15 PM
You need to use instances of these prefabs in the scene. You can create these instances in the editor itself or at runtime. Then assign these instances to their respective variables for text and image.
Not sure what you mean. I made a UI text and image. Then I made empty game objects and I assigned my ui text and ui image to these empty objects. I dragged and dropped them to "Project". What else I need to do?
After dragging and dropping if your text and image gameobjects are still present in the scene then you need to assign those gameobjects from your scene to your variables and not the prefab from the project tab. If you assign the prefabs from project tab then you need to first instantiate those prefabs in the scene using Instantiate and then set these instantiated gameobject so active.
No, the text and image gameobjects are not present in the scene. They disappear when I make them child of the gameobject. But even if I assign those gameobjects to my variables nothing seems to change.
Your answer
Follow this Question
Related Questions
How to change Image on button click in a prefab that is instantiated 1 Answer
UI components all disappear on Play mode, then stay invisible 2 Answers
SetActive true not working on UI object 2 Answers
Disable background UI whenever a pop up displays 2 Answers
Fitting a parent UI element to a Child Text's Content 2 Answers