• 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 /
This question was closed Jul 30, 2020 at 12:10 AM by afdal27kk for the following reason:

Problem is not reproducible or outdated

avatar image
0
Question by afdal27kk · Jul 29, 2020 at 02:24 PM · animatorgetcomponentstartawakestate machine

StateMachineBehaviour get component on awake ??

 public class StateCheck : StateMachineBehaviour
 {
     Animator anim;
 
     private void Awake()
     {
         // anim = with this animator
     }
 }

i need to get animator on awake, how can i do that ??

Comment
Add comment · Show 1
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 mrmatt1877 · Aug 01, 2020 at 01:10 AM 0
Share

If the animator is attached to another game object you should get it in the start method

 void Start()
 {
     anim = GameObject.Find("YourObjectName").GetComponent<Animator>();
 }

If it is attached to the same game object you could get it in awake like so

 void Awake()
 {
     anim = GetComponent()<Animator>();
 }

A good article on awake vs start: https://www.monkeykidgc.com/2020/07/Unity%20Lifecycle%20Awake%20Vs%20OnEnable%20Vs%20Start.html

I think smillyfaces is trying to recommend that you make your Animator private and make it serializable so that you can drag the object with animator into the field inside the unity editor.

like this article https://www.monkeykidgc.com/2020/07/Encapsulation.html

1 Reply

  • Sort: 
avatar image
0

Answer by smillyfaces · Jul 29, 2020 at 02:36 PM

if the animator is already attached to the game object ur script is on there is no need to have on awake for this function, another way if the animator is not attached is to make the variable at the top either [serilizable] or public.

Comment
Add comment · Show 2 · 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 afdal27kk · Jul 29, 2020 at 02:39 PM 0
Share

what do you mean ??, i want to get component the animator on statemahinebehaviour script,,

avatar image smillyfaces · Jul 29, 2020 at 02:45 PM 0
Share

one more thing u could do is create a private GameObject called animator then type in awake animator = GameObject.Find("insert object tag"); animator.GetComponent<Animator>();

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

175 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 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 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

Why do I get a context error when using GameObject.FindWithTag in Awake/Start function and GetComponent in a function? 1 Answer

What is the best method to animate and evaluate player body with animator? 0 Answers

Issue with animator not working when key is pressed - Script and Animator communication - 2D - MissingComponentException 3 Answers

DontDestroyOnLoad() not calling awake/start again 2 Answers

Object reference becomes null between Awake and Start after scene load 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges