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

    rgarand64

    Joined:
    Feb 17, 2022
    Posts:
    14
    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...
    Robert
    2021.3, openXR, oculus no package, XRIT

    upload_2023-12-6_16-38-13.png
     
  2. inmim

    inmim

    Joined:
    Aug 23, 2019
    Posts:
    23
    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;
    8.    
    9.     void Start()
    10.     {
    11.         pos = this.transform.position;
    12.         rot = this.transform.rotation;
    13.         bouncer = Instantiate(goPrefab, pos, rot);
    14.  
    15.         bouncer.GetComponent<Animator>().Play("Bounce");
    16.     }
    17.  
    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);
    27.          
    28.         }
    29.     }
    30. }
     
  3. rgarand64

    rgarand64

    Joined:
    Feb 17, 2022
    Posts:
    14
    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