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.
See the linked thread, I think you will find it useful
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;
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;
}
}