Friday, May 3, 2024
HomeGame Developmentunity - ReleaseInstance is not deleting the object instantiated from an Addressable

unity – ReleaseInstance is not deleting the object instantiated from an Addressable


I have this simple script, which is working perfectly when I press I and it instantiates an addressable game object. But I i am not sure why it is not deleting it when I press D. I confirmed that on press, the log is showing. I have checked in the build too.

public class LoadAddressable : MonoBehaviour
{
    [SerializeField]
    AssetReferenceGameObject _vegetatoin;
    GameObject _instanceInstantaited;

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.I))
        {
            // _vegetatoin.InstantiateAsync();
            //_vegetatoin.LoadAssetAsync().Completed += OnAddressableLoaded;
            _vegetatoin.InstantiateAsync().Completed += OnAddressableLoaded;
        }
        if (Input.GetKeyDown(KeyCode.D))
        {
            Debug.Log("deleting");
            _vegetatoin.ReleaseInstance(_instanceInstantaited);
        }
    }

    private void OnAddressableLoaded(AsyncOperationHandle<GameObject> handle)
    {
        if(handle.Status == AsyncOperationStatus.Succeeded)
        {
            _instanceInstantaited=  Instantiate(handle.Result);
        }
        else
        {
            Debug.Log("Loading asset failed");
        }
    }
}

I am using ReleaseInstance and its documentation said that it destroys the object.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments