Search Unity

  1. Get all the Unite Berlin 2018 news on the blog.
    Dismiss Notice
  2. Unity 2018.2 has arrived! Read about it here.
    Dismiss Notice
  3. We're looking for your feedback on the platforms you use and how you use them. Let us know!
    Dismiss Notice
  4. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Learn more.
    Dismiss Notice
  6. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  7. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  8. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Can't put public instance in StateMachineBehaviour

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

  1. Mehd

    Mehd

    Joined:
    Apr 29, 2016
    Posts:
    73
    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

    Unity Technologies

    Joined:
    Nov 26, 2012
    Posts:
    1,574
    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:
    73
    Thanks a lot ! I can think of a way now !