Search Unity

  1. Unity 2018.3 is now released.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. We've updated our Terms of Service. Please read our blog post from Unity CTO and Co-Founder Joachim Ante here
    Dismiss Notice
  4. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice

Can't put public instance in StateMachineBehaviour

Discussion in 'Animation' started by Mehd, Aug 7, 2018.

  1. Mehd


    Apr 29, 2016
    Hello y'all !

    i'm making a demo in which I need to reference some variables in animation states. This process works like a charm when I'm using string or any numeric values. However, I can't slide any scene GameObject in the field.

    In particular, I want to activate and deactivate a TrailRenderer in when entering and exiting a state. Here's what I got so far:

    Code (CSharp):
    1. public class ActivateEffects : StateMachineBehaviour {
    3.     // public TrailRenderer [] trails;
    4.     public GameObject trail;
    6.      // OnStateEnter is called when a transition starts and the state machine starts to evaluate this state
    7.     override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
    8.         Switch(true);
    9.     }
    11.     // OnStateExit is called when a transition ends and the state machine finishes evaluating this state
    12.     override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) {
    13.         Switch(false);
    14.     }
    16.     void Switch(bool state)
    17.     {
    18.         // foreach(TrailRenderer t in trails)
    19.         // {
    20.         //     t.enabled = state;
    21.         // }
    22.         trail.GetComponent<TrailRenderer>().enabled = state;
    23.     }
    24. }
    Could someone help me on this ?
    Thanks !
  2. Mecanim-Dev


    Unity Technologies

    Nov 26, 2012
    unfortunately this is expected because the statemachine in this case is an asset stored in a controller and in unity you cannot reference a scene object from an asset.

    The only way to setup something like this is at runtime when you start your level you need to initialize all thoses scene reference.

    Usually user will create a Monobehaviour on their root gameobject which they can setup with scene reference like your trail renderer and they override the MonoBehaviour.Start() function to setup all their scene reference into their SMB with
    Mehd likes this.
  3. Mehd


    Apr 29, 2016
    Thanks a lot ! I can think of a way now !