Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
1
Question by Sloth57 · Jun 15, 2016 at 06:55 PM · erroranimator

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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"
Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Sloth57 · Jun 15, 2016 at 11:12 PM 0
Share

Still receive the same error

avatar image TBruce Sloth57 · Jun 15, 2016 at 11:23 PM 0
Share

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.

avatar image Sloth57 · Jun 16, 2016 at 04:31 PM 0
Share

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'

avatar image TBruce Sloth57 · Jun 16, 2016 at 05:28 PM 0
Share

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]);
 }
avatar image Sloth57 TBruce · Jun 16, 2016 at 08:01 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

69 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges