-1

So I am trying to see when my Renderer is deactivated so it activates so it ends the game, but the error in the title appears. Would appreciate some help! The error appears in the if statement if (Rendere.enabled==true) PS.(i know I can take the ==true out but it was to make sure).

private bool lost;
private MeshRenderer Rendere;    


Void Start(){
    Rendere = gameObject.GetComponent<MeshRenderer>();
    lost = false;
    Rendere.enabled = true;
}


void Update()
    {
        if (timer < Time.time) 
        {
            if (Rendere.enabled == true){
                Debug.Log("You lost");
                lost = true;
            }
    }
     }

   private void OnMouseDown()
    {
        Rendere.enabled = false;
    }
Edgar Tip
  • 43
  • 2
  • 7
  • Either (a) `Start` is running after `Update`, so `Rendere` never gets its value, or (b) `gameObject.GetComponent()` is returning a null object, or (c) both. –  Feb 17 '20 at 19:37
  • 1
    @Amy The Start function never runs after Update, see the [execution order](https://docs.unity3d.com/Manual/ExecutionOrder.html). – Philipp Lenssen Feb 17 '20 at 19:49
  • @PhilippLenssen I know it doesn't. You just ruled out (A). –  Feb 17 '20 at 19:51
  • 1
    The error sounds like this particular gameObject doesn't have a meshRenderer. Can you confirm it does? – Philipp Lenssen Feb 17 '20 at 19:52
  • 1
    By the way, you spelled "void Start" as "Void Start", which would return an error. Can you confirm it's just in your code sample here? – Philipp Lenssen Feb 17 '20 at 19:55
  • Yes it is just a code sample! I checked and my cube does indeed have a MeshRenderer. The code runs has intended, but i get 500 messages of the error above. – Edgar Tip Feb 18 '20 at 12:42

1 Answers1

0

Found the error. It seems that only some of my cube objects were following the prefab. If you get this error make sure that they are following the main prefab or do it like me and delete all the same objects in the game and put them from the prefab again. Thanks for all the suggestions!

Edgar Tip
  • 43
  • 2
  • 7