- Home /
How do i make my canvas appear for few seconds only?
I am creating an environment of forest where some enemy soldiers will be running here and there and subject has to observe them.After few seconds of observation, The questions will be asked like behind which trees soldiers are hiding.The subject(who is playing) has to click the trees.If he has clicked correct tree then a canvas which shows a text as "correct" will appear and again after clicking the next tree,the correct/incorrect canvas should appear.
I want the canvas to appear for few seconds.I have tried many codes but they are not useful.Please someone help me out.It would be very thankful for me.
Thanks in advance.
Here is my code that i am using:
using UnityEngine; using UnityEngine.UI; using System.Collections; using UnityEngine.SceneManagement;
public class onclick : MonoBehaviour { Ray ray; RaycastHit hit; Vector3[] o = new Vector3[2]; public GameObject correctCanvas; public GameObject incorrectCanvas; int flag; int i = 0; void Start() {
}
public bool V3Equal(Vector3 a, Vector3 b)
{
return Vector3.Magnitude(a - b) < 10;
}
// Update is called once per frame
void Update()
{
o[0] = new Vector3(304f, 0f, 282.7f);
o[1] = new Vector3(299.6f, 0f, 318.5f);
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
if (Input.GetMouseButtonDown(0))
{
i++;
int j = 0;
flag = 0;
for (; j < 2; j++)
{
if (V3Equal(new Vector3(hit.point.x, hit.point.y, hit.point.z), o[j]))
{
flag = 1;
}
}
if (flag == 1)
{
incorrectCanvas.SetActive(false);
correctCanvas.SetActive(true);
StartCoroutine(LateCall());
}
else if (flag == 0)
{
correctCanvas.SetActive(false);
incorrectCanvas.SetActive(true);
StartCoroutine(LateCall());
}
}
}
}
IEnumerator LateCall()
{
yield return new WaitForSeconds(10);
}
}