- Home /
How do I show/hide gameobjects through script?
I have a gameobject that needs to be shown when I press the key "i". I have been stuck on it for a while now. I have tried using " set active = true/flase" However this doesn't seem to work and the console tells me that it needs to get the API's which I allow however their nothing is working. You guys have any ideas that that is new or can I improve my script. Thank you in advance. :)
Hi, I have tried your suggestions and they don't seem to work. In the script " GetComponent..." the console says nothing is wrong yet nothing happens. Here's my script:
void Update () {
if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.Z))
{
GetComponent<Renderer>().enabled = true;
}
if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.X))
{
GetComponent<Renderer>().enabled = false;
}
I have no idea whats wrong. However are there any components I should add to my character. I think that is the main reason Its not working because it has nothing attached to it. Thank you so much in advance. You responded so quickly and I really appreciate it.
Answer by Halfbiscuit · May 20, 2015 at 09:36 AM
Well in C# you can use gameObject.SetActive(false/true)
to enable or disable the object.
If you are listening for inputs in the Update function you might just want to disable the renderer using:
GetComponent()<Renderer>.enabled = true/false;
thanks, but when I want to enable/disable any other gamobjects trough this script, how does that work? I've tried to put the name on it, but it didn't work. any ideas?
Answer by sgriff08 · May 20, 2015 at 11:04 AM
See the linked thread, I think you will find it useful :)
http://answers.unity3d.com/questions/14165/show-and-hide-a-prefab-or-gameobject.html
This answers page is using unity 4, if you want to access a renderer in Unity 5 you will need to use GetComponent.
Answer by Arshww · Apr 19, 2020 at 06:46 PM
Hello, let me suggest you this:
///This script must be attached on a gameobject wich won't be deactivated
//If you attach this script to gameobject wich will be deactivated then will stop working
public GameObject _Object; // Attach your GameObject you'd like to active/deactive in the inspector
bool isRendererEnabled = true; //This var is only useful if you are working activating/deactivating gameobject's renderer
void Update()
{
//Code below is if you'd like to activate/deactivate the whole GameObject || DELETE IT IF YOU PREFER CODE BELOW
if(_Object.activeInHierarchy && Input.GetKeyDown(KeyCode.I)) //Check if GO is active and if pressed key i
{
_Object.SetActive(false); //If so, then deactivate it
}else if(!_Object.activeInHierarchy && Input.GetKeyDown(KeyCode.I)) //Check if GO is deactivated and if pressed key i
{
_Object.SetActive(true);//If so, then activate it
}
//And the code below is if you'd like to activate/deactivate only gameobject's renderer || DELETE IT IF YOU PREFER CODE ABOVE
if(Input.GetKeyDown(KeyCode.I) && isRendererEnabled)
{
_Object.Component.GetComponent<Renderer>().enabled = false;
isRendererEnabled = false;
}else if(Input.GetKeyDown(KeyCode.I)&& !isRendererEnabled)
{
_Object.Component.GetComponent<Renderer>().enabled = true;
isRendererEnabled = true;
}
}
Your answer
Follow this Question
Related Questions
2D 360 degress platformer example needed 0 Answers
Simulating the Character Controller? 0 Answers
Icy / Slippery surface? 1 Answer
Wheel Collider Tilt? 1 Answer