- Home /
i am trying to deactivate an image for 5 second then make it reappear when the space bar is pressed can anyone help,I am trying to deactivate an image for 5 seconds when I press the space bar and anyone help
this is what i got so far using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Hint : MonoBehaviour {
public GameObject TextObj;
float TmStart;
public float TmLen = 5f;
// Use this for initialization
void Start()
{
TmStart = Time.time;
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Escape))
{
TextObj.SetActive(false).TmLen;
}
}
}
Answer by MT369MT · May 31, 2018 at 03:16 PM
Hi, Do you want to make it reappear after 5 seconds or if you press the space bar? Anyway you can try this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Hint : MonoBehaviour {
public GameObject TextObj;
float TmStart;
public float TmLen = 5f;
// Use this for initialization
void Start()
{
TmStart = Time.time;
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown("space") && TextObj.activeSelf == true)
{
TextObj.SetActive(false);
StartCoroutine(Hide(TmLen));
}
}
IEnumerator Hide(float TmLen)
{
yield return new WaitForSeconds(TmLen);
TextObj.SetActive(true);
}
}
Hey, not here to help (sorry) I just wanted to ask what TmStart is used for and why you set it to Time.time. Does it keep something from breaking or did you just accidentally put it in there?
no, I copied someone's else's script and tried to modify it (badly)
Answer by sampenketh1 · May 31, 2018 at 03:28 PM
sorry didn't make it clear when you press the spacebar it deactivates the image for 5 seconds @MT369MT
Then the code that I posted should be right. Test it and say if it works.
it makes the image deactivate but it doesn't reappear
Where did you attach your script? At the same object that you will make diseappear? Note that if you use .SetActive(false) on an object all components on that object will be disabled and also all script attached on that object won't run anymore.
thanks, I attached it to the canvas the image was on, and it worked
Answer by thekinghamza · May 31, 2018 at 05:53 PM
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Hint : MonoBehaviour {
// if there is a typing error Sorry I didnt write this in VS and if there is a problem with the script add a coment
public GameObject TextObj;
float TmStart;
public float TmLen = 5f;
private Renderer rend;
// Use this for initialization
void Start()
{
TmStart = Time.time;
rend = TextObj.GetComponent<Renderer>();
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown("space") && rend.enabled== true)
{
StartCoroutine(Hide(TmLen));
}
}
IEnumerator Hide(float TmLen)
{
rend.enabled = false;
yield return new WaitForSeconds(TmLen);
rend.enabled = true;`
}
}
you can put this on the object that you want to dissapear or not
Your answer
![](https://koobas.hobune.stream/wayback/20220612163801im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
UI Collision Detection 0 Answers
Find one image into another image 1 Answer
I have overrided a built-in script accidentally and not sure how to fix 2 Answers
Image Filling To Obtain Color Scale 0 Answers
Time Code Web Camera Image Capture 1 Answer