Animation Object

Hi guys I’m try to animate one object when I press “Q” inside of a trigger, but my script doesn’t work, how can I do?

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class animador : MonoBehaviour
{
Boolean puertaabierta;

private void OnTriggerStay(Collider other)
{
if (other.tag == “actmodulo”) {
if (Input.GetKeyDown(KeyCode.Q))
mueble.GetComponent().Play(“modulo”);
if (puertaabierta == true)
puertaabierta = false;
if (puertaabierta == false)
puertaabierta = true;

}
}

// Start is called before the first frame update
void Start()
{
puertaabierta = false;
}

// Update is called once per frame
void Update()
{

}

public GameObject mueble;

}

Hey and welcome. For one you can post code using code tags, so others can read it. Using a language others understand is also a good idea. As a rule of thumb, coding should be done in english, at least if anybody else is ever going to read the code who is not native in the used language. Which is mostly always. While you are at it, using coding conventions so your code is uniform with others (improving readability and maintainability) is also a good idea. Class, method and property names should be written using UpperCamelCase, while variables and such should be written using normal camelCase.

With that out of the way, let’s take a look at your problem. Start by using Debug.Log() to check if your OnTriggerStay and the contained if statements actually get entered to see where the problem lies. Unity - Scripting API: Debug.Log

Edit: Also code is executed sequentially. Currently you do something like “if bool true, bool = false” and after that you do “if bool false, bool = true”, which means the bool will always be true at the end of your OnTriggerStay, which is probably not intended and may already be your problem. If you want one or the other to happen, take a look at ‘else if’.

Ok, I try the debuglog and the script is now this, Its good?

public class animador : MonoBehaviour
{
    Boolean  opendoor;

    private void OnTriggerStay(Collider other)
    {
        Debug.Log("inside");

        if (other.tag == "actmodulo") {
            if (Input.GetKeyDown(KeyCode.Q))
               forniture.GetComponent<Animation>().Play("modulo");
            if (opendoor == true)
                opendoor = false;
            else if
                (opendoor == true);
                  

        }
    }


    // Start is called before the first frame update
    void Start()
    {
      opendoor = false;
    }

    // Update is called once per frame
    void Update()
    {
      
    }

 
    public GameObject forniture;
  

}