Issue checking for a state

just got an issue checking for state, if the state is in the idle state then the move box code should run, currently its not for some reason? i = AnimatorStateInfo

void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.name == "Game_Cube_red" || other.gameObject.name == "Game_Cube_Green")// if red or green box is on trigger
        {
            //Debug.Log("The state name is", i.IsName);
            if (i.IsName("Idle_State"))//if the Arm is not moving
            {
                //move the box
                box_target = other.gameObject;
                is_parrent = true; //set back to false in animation
                anim.SetTrigger("ReachDown");
            }
        }
    }

try these debugs, and tell us what gets printed.

void OnTriggerEnter(Collider other)
    {
        Debug.Log(name + " hit an object. The Object was " + other.transform.name);
        if (other.gameObject.name == "Game_Cube_red" || other.gameObject.name == "Game_Cube_Green")// if red or green box is on trigger
        {
            Debug.Log("Green or Red Hit confirmed!");
            Debug.Log("The current state of i.IsName : " + i.IsName);
            //Debug.Log("The state name is", i.IsName);
            if (i.IsName("Idle_State"))//if the Arm is not moving
            {
                Debug.Log("Idle_State Confirmed");
                //move the box
                box_target = other.gameObject;
                is_parrent = true; //set back to false in animation
                anim.SetTrigger("ReachDown");
            }
        }
    }

I cant add this line
Debug.Log("The current state of i.IsName : " + i.IsName);

Unity throws an error saying:
Operator + cannot be applied to operands of type ‘string’ and ‘method group’

Just thought i would post the full code just in case their is something wrong further up.

public class RobotArm_AI : MonoBehaviour
{

    public bool is_parrent = false;
    public GameObject box_target;
    public Animator anim;
    public Animation animm;
    public Transform newParent;
    //public bool state_idle = true;
    AnimatorStateInfo i;

    // Use this for initialization
    void Start()
    {
        //check the state
        // anim = GetComponent<Animator>();
    }

    // Update is called once per frame
    private void Update()
    {
        AnimatorStateInfo i = anim.GetCurrentAnimatorStateInfo(0);//Layer 1
     
        if (!Input.GetButtonDown("Fire1"))
        {
            //is_parrent =
        }

            if (is_parrent)
        {

            box_target.transform.SetParent(newParent); //passing in the transform and object name in the function
           
        }
        else //not parrent to Arm
        {
          
            box_target.transform.parent = null;//unparrent the gameobject
            box_target = null;
               
        }

        if(i.IsName("Robot_return"))                             //if (animm["Robot_putAway"].time >= 0.9f)
        {
            is_parrent = false;
        }
    }


    void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.name == "Game_Cube_red" || other.gameObject.name == "Game_Cube_Green")// if red or green box is on trigger
        {
            //Debug.Log("The state name is", i.IsName);
            Debug.Log("The current state of i.IsName : " + i.IsName); // this throws an error
            if (i.IsName("Idle_State"))//if the Arm is not moving
            {
                //move the box
                box_target = other.gameObject;
                is_parrent = true; //set back to false in animation
                anim.SetTrigger("ReachDown");
                //state_idle = false;
            }
        }
    }

}

can you show me the AnimatorStateInfo class. I need to see what IsName is. is it a string, an enum??? I hope it’san enum.

if it is you want to do something like

if (i.IsName() == AnimatorStateInfo.IsName.Idle_State)

its a built in struct in unity Unity - Scripting API: AnimatorStateInfo
i am using it to check the name of the current state. Isname is a string

You created a local variable for ‘i’ in Update, rather than using the class level one.

So it worked by making i public changing line 10 into > public AnimatorStateInfo i;
so i guess that means that the On-trigger method wasn’t picking it up then?
Great thanks for the help :slight_smile: i will let you know if i have any other issues

Is line #22 now?:

i = anim.GetCurrentAnimatorStateInfo(0);//Layer 1

When you had ‘AnimatorStateInfo’ in front of ‘i’ on that line, it meant that ‘i’ was valid only inside the Update() method.

Yeah since the variable was local.