Popup text in certain area when clicked
Hi guys,
I am completely new to Unity and would like some help.
I am trying to make a script / action in C# where if a button is clicked a text will appear for about 0.5 seconds within a certain area and then disappear.
Are you able to point me to the right direction or possibly give me a step by step guide on what to write? or a code with //comments above that i can follow and understand what is going on.
Thanks
That's a really generalised question, and very little to go on but what you need is to follow some of the tutorials.
https://unity3d.com/learn/tutorials/topics/user-interface-ui
Chances are you'll be able to do it yourself after following a few of them (and actually working along with them) but if not you can come back with a more specific question.
The tutorials are really good.
*White = Size of Screen *Blue = Area of which pop up text should come up only
Hi, thanks for the response. Hopefully this picture that i drew can clear it up.
user clicks on button
"pop up message" appears in the blue area and disappears after set amount of time
so i think i need something like this on the button
void Update () {
if (GetButtonDown("Fire1"))
{
//Code that instantiates my text into the the blue area with a random position
}
}
I tried this. When i click the button i can see anew Text(Clone) spawn but even though it has Text within the body it does not show.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class OnClick : $$anonymous$$onoBehaviour {
public Text popUP;
private Vector2 position = new Vector2(0,5);
public void PopUP ()
{
Instantiate(popUP, position, Quaternion.identity);
}
}
If it's a Text, you need to put him child of a Canvas :
public GameObject canvas
popup.transform.setParent(canvas)
i get error
Cannot convert from UnityEngine.GameObject to UnityEngine.Transform
Answer by EpiFouloux · Jun 14, 2016 at 10:53 AM
EDITED Well try something this way:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System;
public class OnClick : MonoBehaviour {
public GameObject popUP; // prefab to instantiate
public Canvas canvas; // father canvas
private Vector2 position = new Vector2(0,0);
void Start()
{
position.x = Random.Range(-10f, 10f);
position.y = Random.Range(-10f, 10f);
}
public void PopUP ()
{
GameObject inst = (GameObject) Instantiate(popup, position, Quaternion.identity);
inst.transform.SetParent(canvas.transform);
Destroy(inst, 1.0f);
}
}
Your button must have this script linked and activate the Popup function.
You'll need
using UnityEngine.UI;
rather than import for C#
I currently have 2 Canvases, Canvas 1 that i was unable to change the size of (the parent canvas) and a child canvas 2 where i can change the size, which is inside Canvas 1 (the child canvas).
I tried
position.x = Random.Range(-Screen.maxwidth, Screen.maxwidth);
position.y = Random.Range(-Screen.maxheight, Screen.maxheight);
but the "pop up texts" go all over the place ins$$anonymous$$d of just Canvas 2