Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Issue checking for a state

Discussion in 'Scripting' started by Hybromech, May 2, 2018.

  1. Hybromech

    Hybromech

    Joined:
    Nov 25, 2017
    Posts:
    11
    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


    Code (CSharp):
    1. void OnTriggerEnter(Collider other)
    2.     {
    3.         if (other.gameObject.name == "Game_Cube_red" || other.gameObject.name == "Game_Cube_Green")// if red or green box is on trigger
    4.         {
    5.             //Debug.Log("The state name is", i.IsName);
    6.             if (i.IsName("Idle_State"))//if the Arm is not moving
    7.             {
    8.                 //move the box
    9.                 box_target = other.gameObject;
    10.                 is_parrent = true; //set back to false in animation
    11.                 anim.SetTrigger("ReachDown");
    12.             }
    13.         }
    14.     }
     
  2. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    try these debugs, and tell us what gets printed.
    Code (CSharp):
    1. void OnTriggerEnter(Collider other)
    2.     {
    3.         Debug.Log(name + " hit an object. The Object was " + other.transform.name);
    4.         if (other.gameObject.name == "Game_Cube_red" || other.gameObject.name == "Game_Cube_Green")// if red or green box is on trigger
    5.         {
    6.             Debug.Log("Green or Red Hit confirmed!");
    7.             Debug.Log("The current state of i.IsName : " + i.IsName);
    8.             //Debug.Log("The state name is", i.IsName);
    9.             if (i.IsName("Idle_State"))//if the Arm is not moving
    10.             {
    11.                 Debug.Log("Idle_State Confirmed");
    12.                 //move the box
    13.                 box_target = other.gameObject;
    14.                 is_parrent = true; //set back to false in animation
    15.                 anim.SetTrigger("ReachDown");
    16.             }
    17.         }
    18.     }
     
  3. Hybromech

    Hybromech

    Joined:
    Nov 25, 2017
    Posts:
    11
    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'
     
  4. Hybromech

    Hybromech

    Joined:
    Nov 25, 2017
    Posts:
    11
    Just thought i would post the full code just in case their is something wrong further up.

    Code (CSharp):
    1. public class RobotArm_AI : MonoBehaviour
    2. {
    3.  
    4.     public bool is_parrent = false;
    5.     public GameObject box_target;
    6.     public Animator anim;
    7.     public Animation animm;
    8.     public Transform newParent;
    9.     //public bool state_idle = true;
    10.     AnimatorStateInfo i;
    11.  
    12.     // Use this for initialization
    13.     void Start()
    14.     {
    15.         //check the state
    16.         // anim = GetComponent<Animator>();
    17.     }
    18.  
    19.     // Update is called once per frame
    20.     private void Update()
    21.     {
    22.         AnimatorStateInfo i = anim.GetCurrentAnimatorStateInfo(0);//Layer 1
    23.      
    24.         if (!Input.GetButtonDown("Fire1"))
    25.         {
    26.             //is_parrent =
    27.         }
    28.  
    29.             if (is_parrent)
    30.         {
    31.  
    32.             box_target.transform.SetParent(newParent); //passing in the transform and object name in the function
    33.            
    34.         }
    35.         else //not parrent to Arm
    36.         {
    37.          
    38.             box_target.transform.parent = null;//unparrent the gameobject
    39.             box_target = null;
    40.                
    41.         }
    42.  
    43.         if(i.IsName("Robot_return"))                             //if (animm["Robot_putAway"].time >= 0.9f)
    44.         {
    45.             is_parrent = false;
    46.         }
    47.     }
    48.  
    49.  
    50.     void OnTriggerEnter(Collider other)
    51.     {
    52.         if (other.gameObject.name == "Game_Cube_red" || other.gameObject.name == "Game_Cube_Green")// if red or green box is on trigger
    53.         {
    54.             //Debug.Log("The state name is", i.IsName);
    55.             Debug.Log("The current state of i.IsName : " + i.IsName); // this throws an error
    56.             if (i.IsName("Idle_State"))//if the Arm is not moving
    57.             {
    58.                 //move the box
    59.                 box_target = other.gameObject;
    60.                 is_parrent = true; //set back to false in animation
    61.                 anim.SetTrigger("ReachDown");
    62.                 //state_idle = false;
    63.             }
    64.         }
    65.     }
    66.  
    67. }
     
  5. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    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)
     
  6. Hybromech

    Hybromech

    Joined:
    Nov 25, 2017
    Posts:
    11
  7. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    You created a local variable for 'i' in Update, rather than using the class level one.
     
  8. Hybromech

    Hybromech

    Joined:
    Nov 25, 2017
    Posts:
    11
    So it worked by making i public changing line 10 into >
    Code (CSharp):
    1. public AnimatorStateInfo i;
    so i guess that means that the On-trigger method wasn't picking it up then?
    Great thanks for the help :) i will let you know if i have any other issues
     
    Last edited: May 2, 2018
  9. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Is line #22 now?:
    Code (csharp):
    1. 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.
     
  10. Hybromech

    Hybromech

    Joined:
    Nov 25, 2017
    Posts:
    11
    Yeah since the variable was local.