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 /
avatar image
1
Question by krupps · Dec 07, 2017 at 08:16 PM · gameobjectanimatorbehaviour

Getting a reference to an object from a state behaviour

I have a State Behavior walk and Cry.

When "OnStateEnter" fires from the animator, I want to change some properties on the gameObject that is entering the state.

So when the enemy enters the walk state from the State Behavior overriden method, what is the most efficient way to get a reference to this object that has a script "EnemyScript" attached to it?

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

3 Replies

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

Answer by Hellium · Dec 07, 2017 at 08:20 PM

 using UnityEngine;
 
 public class MyStateBehaviour : StateMachineBehaviour
 {
     override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
     {
         EnemyScript script = animator.GetComponent<EnemyScript>();
         script.DoSomething();
     }
 }
Comment
Add comment · Show 3 · 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 orikalin · May 29, 2020 at 06:43 PM 0
Share

Is there a less gross way of doing this? I am having a similar issue, and I came to this same solution on my own, but I dont want to be doing a GetComponent on every state enter, because thats going to be happening a lot, on many objects, and thats very bad for performance.

I tried serializing a RigidBody2D varaible and dragging in the component on/from the prefab, but I guess this results in every object in the scene referencing the component in the prefab and not its own rigidbody.

avatar image Hellium orikalin · May 29, 2020 at 06:58 PM 0
Share

You could take advantage of interfaces I guess.

SOLUTION NOT TESTED

 public interface Foo<T>
 {
     T Component { get; set; }
 }
 
 [RequireComponent(typeof(Animator))]
 public class Bar : $$anonymous$$onoBehaviour
 {
     [SerializeField] private EnemyScript enemy;
 
     private void Start()
     {
         Foo<EnemyScript>[] foos = GetComponent<Animator>().GetBehaviours<Foo<EnemyScript>>();
         foreach(var foo in foos)
             foo.Component = enemy;
     }
 }


  public class $$anonymous$$yStateBehaviour : State$$anonymous$$achineBehaviour, Foo<EnemyScript>
  {
      public EnemyScript Component { get; set; }

     override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
      {
          Component.DoSomething();
      }
  }



avatar image Fangh orikalin · Oct 29, 2021 at 02:38 PM 0
Share
 using UnityEngine;
 
 public class MyStateBehaviour : StateMachineBehaviour
 {
     private EnemyScript script;
     override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
 {
     if(script == null)
         script = animator.GetComponent<EnemyScript>();
 
     script.DoSomething();
 }
  }
avatar image
1

Answer by Bendixsa · Jun 26, 2020 at 04:28 PM

@krupps You can add a base state behavior script, In other words one that acts on the overall state machine. You do this by just clicking on the background of the animator to deselect any states (the base layer itself).

You will then see in the inspector you can add a behavior (it's the only thing you can do here)

This behavior is special because it has additional event hooks and is persistant to the statemachine making it the perfect place to cache some references and a great place to do higher level persistent logic to control the state machine.

Hope This Helps. Enjoy!

Comment
Add comment · 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
0

Answer by krupps · May 30, 2020 at 09:49 PM

Actually a GetComponent is just a cast so it’s really not that bad

Comment
Add comment · 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

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

112 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

Related Questions

Assign spells/abilities dynamically to a GameObject? 0 Answers

Animation issue (OnTriggerEnter) ***SOLVED*** 1 Answer

Keyframes of animated Child gameobject not shown by parent gameobjects animator issue ? 0 Answers

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

Air jumping animation help 1 Answer


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