- Home /
GameObject turning mesh renderer on/off by pressing F1
Hello, I made a piece of code that I want to turn the mesh renderer on and off by pressing f1, f2, etc. But I get two errors that say the same thing. Here is the code:
#pragma strict
private var showThis : GameObject;
function Start(){
showThis = GameObject.Find("Cube");
}
function Update() {
if(Input.GetKeyDown ("f1"))
{
showThis.GetComponent.MeshRenderer = true;
}
else
{
showThis.GetComponent.MeshRenderer = false;
}
}
Answer by robertbu · Apr 13, 2014 at 10:15 PM
When you post code with compiler errors, please copy and past the error message from the console into your question. Your issue is because this is not the way you get a component. In addition you don't have to use GetComponent() to get the Renderer component. Try this instead:
#pragma strict
private var showThis : GameObject;
function Start(){
showThis = GameObject.Find("Cube");
}
function Update() {
if(Input.GetKeyDown (KeyCode.F1)) {
showThis.renderer.enabled = !showThis.renderer.enabled;
}
}
Thank you so much! And next time I will share my errors too. Thanks so much again!
Your answer
Follow this Question
Related Questions
Mesh renderer does not move with parent rigid body 5 Answers
using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers
how to check for GameObject is null in array with random 1 Answer
prefab is instantiating without a script 2 Answers
how to rotate gameobject only in multiple of 36 degree ? 1 Answer