Hello, I have a specific functionality I wish to input in my AR scene that I am currently struggling to find a solution for. What I want to do is, when a marker is scanned, have both a prefab (that was assigned to said marker via the prefabimagepairmanager script) and a textbox be displayed on the phone and when a button is pressed, have this prefab and textbox removed from the scene. The current script I have to do this is as follows:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.XR.ARSubsystems;
using UnityEngine.XR.ARFoundation;
public class TreasureFound : MonoBehaviour
{
private GameObject treasureImage;
private GameObject treasureText;
public static bool cathedralFound = false;
public static bool footballFound = false;
public static bool universityFound = false;
private DisplayTrophy d;
public AudioSource clip;
public GameObject foundText;
private Button closeButton;
private void Start()
{
d = GameObject.Find("ButtonController").GetComponent<DisplayTrophy>();
if(this.gameObject.tag == "Angel")
{
treasureText = GameObject.Find("CathedralContainer");
treasureText.transform.GetChild(1).gameObject.SetActive(true);
closeButton = treasureText.transform.GetChild(1).gameObject.GetComponent<Button>();
closeButton.onClick.AddListener(() => RemoveFromScene());
if (!cathedralFound)
{
treasureImage = GameObject.Find("CathedralTreasure");
treasureImage.SetActive(false);
cathedralFound = true;
clip.Play();
Handheld.Vibrate();
d.markersFound++;
}
else
{
Instantiate(foundText, foundText.transform.position, foundText.transform.rotation);
Invoke("DestroyText", 2.0f);
}
treasureText.transform.GetChild(0).gameObject.SetActive(true);
}
else if(this.gameObject.tag == "Football")
{
treasureText = GameObject.Find("FootballContainer");
treasureText.transform.GetChild(1).gameObject.SetActive(true);
closeButton = treasureText.transform.GetChild(1).gameObject.GetComponent<Button>();
closeButton.onClick.AddListener(() => RemoveFromScene());
if (!footballFound)
{
treasureImage = GameObject.Find("FootballTreasure");
treasureImage.SetActive(false);
footballFound = true;
clip.Play();
Handheld.Vibrate();
d.markersFound++;
}
else
{
Instantiate(foundText, foundText.transform.position, foundText.transform.rotation);
Invoke("DestroyText", 2.0f);
}
treasureText.transform.GetChild(0).gameObject.SetActive(true);
}
else if(this.gameObject.tag == "Laptop")
{
treasureText = GameObject.Find("UniversityContainer");
treasureText.transform.GetChild(1).gameObject.SetActive(true);
closeButton = treasureText.transform.GetChild(1).gameObject.GetComponent<Button>();
closeButton.onClick.AddListener(() => RemoveFromScene());
if (!universityFound)
{
treasureImage = GameObject.Find("UniversityTreasure");
treasureImage.SetActive(false);
universityFound = true;
clip.Play();
Handheld.Vibrate();
d.markersFound++;
}
else
{
Instantiate(foundText, foundText.transform.position, foundText.transform.rotation);
Invoke("DestroyText", 2.0f);
}
treasureText.transform.GetChild(0).gameObject.SetActive(true);
}
}
private void RemoveFromScene() //should be called when game object is destroyed
{
treasureText.transform.GetChild(0).gameObject.SetActive(false);
treasureText.transform.GetChild(1).gameObject.SetActive(false);
Destroy(this.gameObject);
}
void DestroyText()
{
Destroy(foundText);
}
}
It must likely could be more efficient, but this script is attached to each individual prefab that is associated with each marker. In theory it should check which prefab is instantiated, find and display the relevant textbox and button and when this button is pressed, no longer display the text and destroy the current prefab. Ideally the next time the marker is scanned, it should display the same prefab and text again but also show a message to the user saying it is already scanned.
I initially believed that the marker image might have been instantiating a copy but on testing, this clearly isn’t the case. I’m trying to think how best to ensure that the prefab instantiated can both be removed from the scene and then re-instantiated later using the same marker. Is there any way to do this?