- Home /
Object Visibility
I want to make an object with a Sprite Renderer invisible. When the player presses a key, the object would become visible. How would I do that? Here is my code so far:
using UnityEngine;
public class A1 : MonoBehaviour {
// Use this for initialization
// Update is called once per frame
void Update()
{
if (Input.GetKey("a"))
{
gameObject.SetActive(value: false);
}
if (Input.GetKey("d"))
{
gameObject.SetActive(value: true);
}
}
}
Answer by nicholasw1816 · Dec 08, 2018 at 07:04 PM
Here you go :)
GetComponent<SpriteRenderer>().enabled = false;
lol I had to edit that 3 times in 4 $$anonymous$$ lmao, forgot the code button, wasn't co$$anonymous$$g out right
Do you think you could help me with my other question?
Answer by Sabre-Runner · Dec 08, 2018 at 07:37 AM
GameObjects that are disabled do not get Unity methods called on them.
You need to either keep the GameObject active and disable/enable the sprite renderer or have another object receive the mouse input and control the first object's activation state.
Never $$anonymous$$d I got it. Thanks for your help!!
Your answer
Follow this Question
Related Questions
Objects Appearing 1 Answer
Hide Object in Editor Only 5 Answers
Unity 2019.4.9f1 - Toggle Show/Hide Gameobject with one keystroke. 1 Answer
on collision, show/hide other model scripting ? 1 Answer
Gameobject shown as GUI 1 Answer