Having trouble deleting an InstantiateAsync object

Open
#32 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
25/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
csharp, unity
Domain
game-dev

Research direction

Start by reproducing the issue in the Unity AR sample and trace OnImageUpdated and OnImageAdded. Inspect the Addressables.InstantiateAsync, Release, and ReleaseInstance calls around target and curobject, then verify that changing the detected image removes the previous instance before the new asset is created and parented.

Written by the indexing model from the issue text.

Description

Hi,

I have an AR App where when an Image gets detected, it should Instantiate an AssetReference that image is associated with.

But I have another method where if a new Image is detected, I want to first delete the previously instantiated AssetReference, and then instantiate the AssetReference associated with the new Image.

Here is my code:

`
public class MultiTrackedImageManager : MonoBehaviour
{

public static MultiTrackedImageManager Instance { get { return instance; } }
private static MultiTrackedImageManager instance;

private ARTrackedImageManager m_TrackedImageManager;
private GameObject curobject;
private string PREFABFOLDER = "Prefabs/";

AssetBundle myLoadedAssetBundle;
[SerializeField]
private string imageName;
private GameObject prefabToLoad;

public List<MultiTrackedImage> Images = new List<MultiTrackedImage>();

// Addressables necessities for test
GameObject target;
private Vector3 spawnPosition;
GameObject testObj;

private void Awake()
{
    //if (instance != null) { Destroy(instance); }
    instance = this;
    m_TrackedImageManager = GetComponent<ARTrackedImageManager>();
    curobject = null;
    spawnPosition = Vector3.zero;
}
void OnEnable()
{
    m_TrackedImageManager.trackedImagesChanged += OnTrackedImagesChanged;
}

void OnDisable()
{
    m_TrackedImageManager.trackedImagesChanged -= OnTrackedImagesChanged;
}

void OnTrackedImagesChanged(ARTrackedImagesChangedEventArgs eventArgs)
{

    foreach (var trackedImage in eventArgs.updated)
    {
        OnImageUpdated(trackedImage.referenceImage.texture.name, trackedImage.name, trackedImage);
    }

    foreach (var trackedImage in eventArgs.added)
    {
        OnImageAdded(trackedImage.referenceImage.texture.name, trackedImage.name, trackedImage);
    }
}

public void OnImageUpdated(string textureName, string name, ARTrackedImage m_ARTrackedImage)
{
    
    for (int i = 0; i < Images.Count; i++)
    {
        //if we have a match for the object and we have regained tracking and the current object is not the one for the trigger we just found
        if (m_ARTrackedImage.trackingState == TrackingState.Tracking)
        {

            //this checks to make sure we are tracking the right image target
            if(!string.Equals(Images[i].target.name, textureName)) { continue; }

            //this checks to see if our current object is equal to the name of the image target so we don't delete and duplicate
            if (string.Equals(curobject.name, Images[i].target.name)) { continue; }

            //for some reason we are getting updates from non visible targets
            if (curobject != null) { Destroy(curobject);}

            GameObject go = GameObject.Find(name);

            //Instantiate the prefab with the position of go
            var addresableTarget = Addressables.InstantiateAsync(Images[i]._prefabReference, go.transform.position, Quaternion.identity);

            if(target != null) {Addressables.Release(target); Addressables.ReleaseInstance(addresableTarget); }
            target = addresableTarget.Result;

            curobject = target;
            curobject.name = Images[i].target.name;

            TextManager.s.txtContent1 = Images[i].txt1 == "" ? "  " : Images[i].txt1;
            TextManager.s.txtContent2 = Images[i].txt2 == "" ? "  " : Images[i].txt2;
            TextManager.s.ResetText();

            Images[i].go = go;
            target.transform.rotation = go.transform.rotation;
            target.transform.SetParent(go.transform);
            target.transform.localPosition = Vector3.zero;
        }
    }
}

public void OnImageAdded(string textureName, string name, ARTrackedImage m_ARTrackedImage)
{

    //PrefabManager.s.ClearPrefabs();
    for (int i = 0; i < Images.Count; i++)
    {
        if (string.Equals(Images[i].target.name, textureName) && Images[i].go == null)
        {
            //Destroy the existing AR Animation
            if (curobject != null) { Destroy(curobject);}

            GameObject go = GameObject.Find(name);

            //Instantiate the prefab with the position of go
            var addresableTarget = Addressables.InstantiateAsync(Images[i]._prefabReference, 
go.transform.position, Quaternion.identity);

            if (target != null) { Addressables.Release(target); Addressables.ReleaseInstance(addresableTarget); }
            target = addresableTarget.Result;

            curobject = target;
            curobject.name = Images[i].target.name;

            TextManager.s.txtContent1 = Images[i].txt1 == "" ? "  " : Images[i].txt1;
            TextManager.s.txtContent2 = Images[i].txt2 == "" ? "  " : Images[i].txt2;
            TextManager.s.ResetText();

            Images[i].go = go;
            target.transform.rotation = go.transform.rotation;
            target.transform.SetParent(go.transform);
            target.transform.localPosition = Vector3.zero;
        }
    }
}

`

Dominant language
C#
Stars
1.5k
Forks
303
PR merge metrics
No merged PRs in 30d

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from Unity-Technologies/Addressables-Sample

All issues in Unity-Technologies/Addressables-Sample

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.