Having trouble deleting an InstantiateAsync object
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 25/100
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
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from Unity-Technologies/Addressables-Sample
-
Difficulty 4/5 3-5 days Newbie friendliness 32/100
-
Difficulty 4/5 3-5 days Newbie friendliness 25/100
Unity-Technologies/Addressables-Sample#98 · 3 comments ·
-
Difficulty 4/5 3-5 days Newbie friendliness 35/100
Unity-Technologies/Addressables-Sample#94 · 2 comments ·
-
Difficulty 4/5 3-5 days Newbie friendliness 25/100
-
Difficulty 3/5 1-2 days Newbie friendliness 30/100
Unity-Technologies/Addressables-Sample#90 · 1 comment ·
All issues in Unity-Technologies/Addressables-Sample
Similar issues
-
bug
Difficulty 1/5 Under an hour Newbie friendliness 75/100
sillsdev/languageforge-lexbox#2665 ·
-
bug documentation frontend
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
azurenoops/spin_agent#975 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
-
:watch: Not Triaged 11.0 fundamentals/subsvc
Difficulty 2/5 1-3 hours Newbie friendliness 92/100
dotnet/AspNetCore.Docs#37699 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
SubtitleEdit/subtitleedit#15108 · 1 comment ·