Search Unity

Can't assign an image to script in animator

Discussion in 'Scripting' started by MrScamander, Jan 18, 2020.

  1. MrScamander

    MrScamander

    Joined:
    Jan 14, 2020
    Posts:
    1
    I've written a scrip which disables an image when an animation ends, but i can't assign the image to animator

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3.  
    4. public class afterFade : StateMachineBehaviour
    5. {
    6.     public Image image;
    7.     override public void OnStateExit(UnityEngine.Animator animator, UnityEngine.AnimatorStateInfo animatorStateInfo, int layerIndex)
    8.     {
    9.         image.gameObject.SetActive(false);
    10.     }
    11. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,749
    I confirmed this behavior with a quick test, and now I realize why. Read here:

    https://forum.unity.com/threads/cant-put-public-instance-in-statemachinebehaviour.544202/

    You are dragging a scene-based object into that field, and that field is stored in the actual animator asset on disk. You can't have a disk asset referring to something as transient as a scene object, in the same way you can't drag references to an in-scene prefab into a not-in-scene asset's property field.