- Home /
 
How to active mesh renderer with a script C#
I need to activate a mesh on an object that i got. and i want it to be activated when a certain condition is met. This is my code for the score
 public class counter : MonoBehaviour {
 
     public static int scoreCounter = 0;
     
     
     void OnGUI()
     {
         string counterText = "Score is: " + scoreCounter;
         GUI.Box (new Rect(Screen.width - 150, 20, 130, 20), counterText);
     }
 }
 
               And this is the code i got so far to enable the mesh.
 public class Renderpart2 : MonoBehaviour {
 
     // Use this for initialization
     void Start () {
         GameObject Score = GameObject.Find("Score");
         counter counter = Score.GetComponent<counter>();
         if(counter.scoreCounter > 40) {
         GetComponent(MeshRenderer).enabled = true;
         }
     }
 
 }
 
               Thank you for your help.
You need to use a reference to the gameobject you are trying to access when using GetComponent($$anonymous$$eshRenderer).enabled = true;.
Something like: Score.renderer.enabled = true 
Answer by ZDS Alpha · Nov 04, 2013 at 02:44 PM
 using UnityEngine;
 using System.Collections;
 
 public class Enabler : MonoBehaviour {
     public GameObject Obj;
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
         MeshRenderer m =Obj.GetComponent<MeshRenderer>();
         m.enabled = true;
     }
 }
 
               or like this
 using UnityEngine;
 using System.Collections;
 
 public class Renderpart2 : MonoBehaviour {
  
     // Use this for initialization
     void Start () {
        GameObject Score = GameObject.Find("Score");
        counter counter = Score.GetComponent<counter>();
        if(counter.scoreCounter > 40) {
        GetComponent<MeshRenderer>().enabled = true;
        }
     }
  
 }
 
              I tried to use the second code, but it doesn't seem to work. I don't get any error. The first code I don't see any sense in. what is the "m" and i don't see any reference to the scoreCounter variable.
Answer by g0tNoodles · Nov 05, 2013 at 12:35 PM
You need to use a reference to the gameobject you are trying to access when using
 GetComponent(MeshRenderer).enabled = true;
 
               Use something like this instead:
 Score.renderer.enabled = true
 
               or
 counter.renderer.enabled = true
 
              Your answer