• 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
5
Question by NGC6543 · Jul 02, 2015 at 12:25 PM · state machine

Can't assign active gameobjects to public variables in StateMachineBehaviour script from the inspector screen

Hi, I need some help!!

I made an animation controller, added some states, and added StateMachineBehaviour in one of the states. I wanted to change texture when it enters certain state so I wrote some code inside the StateMachineBehaviour.

 public class Smile : StateMachineBehaviour {
     public Texture2D smileTexture;
     public GameObject[] Eyes;
 
      // OnStateEnter is called when a transition starts and the state machine starts to evaluate this state
     override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
         //FIXME : Why can't I assign the eyes in the inspector?!!!!!!!
 //        eyes[0].GetComponent<Material>().mainTexture = smileTexture;
 //        eyes[1].GetComponent<Material>().mainTexture = smileTexture;
 
     }
 }

It shows error(about Serialized... I don't remember) first, so I searched the solution and one says it should fix when Unity restarts. So I restarted the program and there are no more errors.

now, I can't assign those public gameobject[] eyes. The objects from "Hierarchy"(active gameobject) won't be assigned, while objects from "Project"(assets) will be.

Does StateMachineBehaviour class allow active gameobject?

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
13
Best Answer

Answer by Baste · Jul 02, 2015 at 03:18 PM

StateMachineBehaviours should not be able to link objects from the scene. They are a part of the animator controller, which means that whatever you assign to that field will be assigned in all scenes.

In short, the instance of your StateMachineBehaviour lives inside an asset, not inside a scene.

To fix this, you'll have to have the Eyes linked somewhere else, and get that. You could put this script on the same object as the Animator:

 public class EyeLinker : MonoBehaviour {
     public GameObject[] eyes;
 }

And then do Smile like this:

 public class Smile : StateMachineBehaviour {
     public Texture2D smileTexture;
     private GameObject[] eyes;
 
      // OnStateEnter is called when a transition starts and the state machine starts to evaluate this state
     override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
         if(eyes == null)
             eyes = animator.gameObject.GetComponent<EyeLinker>().eyes;

         ...
     }
 }

Of course, if there's already a script on the Animator, you can add the eyes field there instead.

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 NGC6543 · Jul 03, 2015 at 07:34 AM 0
Share

Thank you for your kind reply. It helped me a lot.

avatar image mr_blahblah · Jul 01, 2016 at 04:49 AM 0
Share

This looks like a great solution, but it's not working for me: I'm doing the following, which doesn't work.

 public class TestingState$$anonymous$$achines : State$$anonymous$$achineBehaviour {
 
     override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
     {
         Debug.Log("Testing"); // THIS NEVER DISPLAYS
         
         var temp = animator.GetComponentInParent<DemoAI>().attackAnimComplete = true; // THIS NEVER GETS SET
 
 
 }
avatar image
1

Answer by flowdee · Nov 21, 2016 at 09:07 AM

quiet late but give a try to:

  var temp = animator.GetComponentInParent<DemoAI>();
 temp.attackAnimComplete = true; 
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 RepoGames · Feb 22, 2017 at 06:44 AM

I think the cleaner way is to use animator.GetBehaviour().OnEnded()+=myCallback and just invoke in the behaviour the callback

Comment
Add comment · Show 1 · 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 harshaxnim · Sep 13, 2018 at 08:25 PM 0
Share

can you please elaborate a little more on this? or point me to some docs...? 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

8 People are following this question.

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

Related Questions

Gun State Machine 1 Answer

How do i use StateMachine logic with my code ? 1 Answer

How to implement the "State" pattern in Unity using an abstract class? 1 Answer

Finite State Machine 0 Answers

Using Unity's state machine GUI 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