Play animation if mesh renderer is enabled

Hello!

I can’t for the life of me figure this out, probably not that difficult but I’m still learning C#.
If anyone can help me out with this, it’d be super helpful so thanks in advance!

I have 3 objects with one child each. Their mesh renderer is disabled, but you can enable it by clicking on the main object. I also have a locked door with an opening animation.
That door is supposed to stay locked until you’ve enabled the mesh renderer of all 3 “child objects”.

I thought something like this would do the trick, and actually I’m not getting any error messages, but it does not work:

void OnTriggerStay () {

        MeshRenderer m =candleLight.GetComponent<MeshRenderer>();
        MeshRenderer n =candleLight_2.GetComponent<MeshRenderer>();
        MeshRenderer o =candleLight_3.GetComponent<MeshRenderer>();


        if(m.enabled == true && n.enabled == true && o.enabled == true)
        {
      
        if (Input.GetMouseButtonDown (2))
            hingeOpen.Play ();

        }
    }

Any ideas on how to fix it? What am I missing here? Again, thank you very much!

I’m curious by the usage of Input.GetMouseButtonDown(2). Is it intended to require the middle mouse button?

my 2 cents

void OnTriggerStay ()
{
    //this is bad for performance so try and avoid doing GetComponent.  it's ok to do them in Awake, or Start
    MeshRenderer m =candleLight.GetComponent<MeshRenderer>();
    MeshRenderer n =candleLight_2.GetComponent<MeshRenderer>();
    MeshRenderer o =candleLight_3.GetComponent<MeshRenderer>();


    if(m.enabled == true && n.enabled == true && o.enabled == true)
    {
    if (Input.GetMouseButtonDown (2))
        hingeOpen.Play ();

    }
   
}

//this is better code, but you're doing the GetComponent,  but you're only doing it once

void OnTriggerStay()
{

    if (Input.GetMouseButtonDown (2)) //i think mouse button 2 is the scroll wheel.
    {
        Debug.Log("Middle mouse button was clicked");
        MeshRenderer m =candleLight.GetComponent<MeshRenderer>();
        MeshRenderer n =candleLight_2.GetComponent<MeshRenderer>();
        MeshRenderer o =candleLight_3.GetComponent<MeshRenderer>();
       
        if(m.enabled == true && n.enabled == true && o.enabled == true) //this means all three renders have to be enabled to play the animation
        {
            hingeOpen.Play ();
        }
    }
}

what part doesn’t work?
Are you getting the Debug prints outs in the console?

Nevermind! I restarted Unity and it worked perfectly!
Thank you so much! :slight_smile: