Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question How to release object from animator ?

Discussion in 'Animation' started by rgarand64, Dec 6, 2023.

  1. rgarand64


    Feb 17, 2022
    Hi All,
    Posted this question in script, no solution. So I try in this thread since it's an animation problem.
    I have a prefab which I animate. But when I destroy prefab's instance, it would not disappear. It keeps being animated. Tried a few ways to solved this to no avail. It works perfectly in Quest2 from editor, but not in the build. Have no build errors, all else working perfectly except that.
    tried animation.enabled = false to release handle on object, runtimeanimatorcontroller- no destroy, quit or similar method...

    Any advice would be welcome, project on hold until solved...
    2021.3, openXR, oculus no package, XRIT

  2. inmim


    Aug 23, 2019
    I've tested a variant of your code shown, and the prefab does get destroyed as soon as I hit the spacebar. The script is placed on an empty GO. the prefab was a simple 3D box with eyes, and bounced up/down, while looking at a moving ball. No errors. Are you sure you are reaching the Destroy part of your script? Add a debug to your "if (!Game_Manager.....){" section.
    Code (CSharp):
    1. public class DestroyPreFab : MonoBehaviour
    2. {
    3.     [SerializeField] private GameObject goPrefab;
    4.     [SerializeField] private Vector3 pos;
    5.     [SerializeField] private Quaternion rot;
    6.     private GameObject bouncer;
    7.     [SerializeField] private Transform tr;
    9.     void Start()
    10.     {
    11.         pos = this.transform.position;
    12.         rot = this.transform.rotation;
    13.         bouncer = Instantiate(goPrefab, pos, rot);
    15.         bouncer.GetComponent<Animator>().Play("Bounce");
    16.     }
    18.     void Update()
    19.     {
    20.         if (bouncer != null)
    21.         {
    22.             bouncer.transform.LookAt(tr);
    23.         }
    24.         if (Input.GetKeyDown(KeyCode.Space))
    25.         {
    26.             Destroy(bouncer);
    28.         }
    29.     }
    30. }
  3. rgarand64


    Feb 17, 2022
    Hi Inmim,
    Thx very much for your time :), Did as you propose. Used plenty of debug.log, but had to go for a runtime debugger, and, as you suggest, the problem was with call to audiosource, had a null reference to volume :(
    BUT, what I don't understand is why it worked in editor on console, and worked for the first 2 levels in runtime, only broke on third level with exact same code, except for animation ( that is why I suspected that right away )

    Anyway, now I can go on, very grateful for your cue, I could have gone in circle for a good while longer.

    Best regards, Robert