Search Unity

Can't put public instance in StateMachineBehaviour

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

  1. Mehd

    Mehd

    Joined:
    Apr 29, 2016
    Posts:
    91
    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 {
    2.  
    3.     // public TrailRenderer [] trails;
    4.     public GameObject trail;
    5.  
    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.     }
    10.  
    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.     }
    15.  
    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. }
    25.  
    Could someone help me on this ?
    Thanks !
     
  2. Mecanim-Dev

    Mecanim-Dev

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    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

    https://docs.unity3d.com/ScriptReference/Animator.GetBehaviours.html
     
    Mehd likes this.
  3. Mehd

    Mehd

    Joined:
    Apr 29, 2016
    Posts:
    91
    Thanks a lot ! I can think of a way now !