- Home /
disabling Mesh renderer on runtime.
I want to turn the mesh renderer off on runtime but for some reason the mesh renderer gets disabled but the object still can be seen; I tried turning the component itself off and also tried turning the renderer of (renderer.enable = false;)
Here is the script Im using, it disables the renderer depending of how far the Player is, I have a similar script for turning lights off that works perfectly
using UnityEngine;
using System.Collections;
public class Betolder : MonoBehaviour {
public float availableDistance;
private float Distance;
///------------------------------
// Update is called once per frame
void Update () {
GameObject Player = GameObject.FindGameObjectWithTag("Player");
MeshRenderer MeshComponent = gameObject.GetComponent<MeshRenderer>();
Distance = Vector3.Distance(Player.transform.position, transform.position);
if (Distance < availableDistance){
renderer.enabled = true;
}
if (Distance > availableDistance){
renderer.enabled = false;
}
}
}
This one works too, the Mesh renderer gets disabled but the object still there like nothing happened, is weird because If I turn the Mesh renderer off in the editor the object its not rendered at all so I dont understand why I cant do the same thing on runtime.
][1]
well, it should, thats why its weird, theres nothing wrong with the script (as far as I know) but the object still gets rendered.
What does it look like in the inspector while the game is running? Is the renderer disabled? Is there something else enabling it?
That does look weird. What if you toggle it back on and off in the inspector while the game is running - does it disappear then?
check out the image I just uploaded, you can see it is off in the inspector but the object still perfectly visible. There nothing turning it on.
Answer by felixpk · Nov 14, 2013 at 02:09 PM
Try gameObject.GetComponentInChildren>Renderer<().enabled = false;
(Inverse these: "") for some reason the code disapears if I write this.
gameObject.GetComponentInChildren<Renderer>().enabled = false;
Format it with "Code Sample" then it doesn't remove them.
Can somebody tell me why this isn't working inside the Start() $$anonymous$$ethod?