Animator().Play is not accepting variable
When I try and run the following to play an animation, I receive the following in Unity
Animator.GotoState: State could not be found UnityEngine.Animator:Play(String) Test:OnMouseDown() (at Assets/Test.cs:27) UnityEngine.SendMouseEvents:DoSendMouseEvents(Int32)
Invalid Layer Index '-1' UnityEngine.Animator:Play(String) Test:OnMouseDown() (at Assets/Test.cs:27) UnityEngine.SendMouseEvents:DoSendMouseEvents(Int32)
public class Test : MonoBehaviour { public GameObject Player;
// Use this for initialization
void Start () {
Player = GameObject.FindWithTag("Player");
}
// Update is called once per frame
void Update () {
}
public void OnMouseDown() {
string i1, i2, d;
int e;
Player = GameObject.FindWithTag("Player");
i1 = "Idle2Crouch_Neutral2Crouch2Idle";
i2 = "Idle_JumpDownLow_FlipOnHands_Idle";
e = Random.Range(1,3);
d = "i" + e;
Player.GetComponent<Animator>().Play(d);
}
Any help would be appreciated
Answer by TBruce · Jun 15, 2016 at 07:17 PM
I am going to assume that
"Idle2Crouch_Neutral2Crouch2Idle"
"Idle_JumpDownLow_FlipOnHands_Idle"
are animator states. If that is the case then change
i1 = "Idle2Crouch_Neutral2Crouch2Idle";
i2 = "Idle_JumpDownLow_FlipOnHands_Idle";
to
i1 = "Base Layer.Idle2Crouch_Neutral2Crouch2Idle"
i2 = "Base Layer.Idle_JumpDownLow_FlipOnHands_Idle"
Do this first
private Animator animator;
void Start()
{
animator = Player.GetComponent<Animator>();
}
Then you may want to try
animator.Play(animator.StringToHash(d));
As I said, I was assu$$anonymous$$g that i1 & i2 are Animator states. If they are animations then you need to change i1 & i2 to use the states that represent the animations.
They are animations inside of the Animator.
If I leave out the base layer. or even with it I can use i1 or i2 with no problems.
But creating the string with a new variable gives the problem.
I tried the suggested and it didn't work I received several errors
Assets/Test.cs(30,40): error CS0176: Static member UnityEngine.Animator.StringToHash(string)' cannot be accessed with an instance reference, qualify it with a type name ins$$anonymous$$d Assets/Test.cs(30,26): error CS1502: The best overloaded method match for
UnityEngine.Animator.Play(string)' has some invalid arguments
Assets/Test.cs(30,26): error CS1503: Argument #1' cannot convert
object' expression to type `string'
I see now.
This will fix the problem
private GameObject Player;
private Animator animator;
private string[] idleAnimations = {"Idle2Crouch_Neutral2Crouch2Idle", "Idle_JumpDownLow_FlipOnHands_Idle"};
void Start()
{
Player = GameObject.FindWithTag("Player");
animator = Player.GetComponent<Animator>();
}
public void On$$anonymous$$ouseDown()
{
int anim = Random.Range(0, 1);
animator.Play(idleAnimations[anim]);
}
Aha, I was thinking I would need to do an array. It's been awhile since I've done any program$$anonymous$$g so my $$anonymous$$d wasn't with me on doing so. That definitely fixed it, well I had to expand the range to (0,2). Thanks!
Your answer
Follow this Question
Related Questions
How can I solve this error? 1 Answer
Unity Editor Animator Not Set To Instance Of Object 0 Answers
How can I fix this error(warning)? 1 Answer
Unity, Mechanim And Mixamo 3D Animation: NullReferenceException. 0 Answers
Unassigned Reference Exception on an Animator, but Animator is assigned in Inspector? 0 Answers