Timer for destroying/ appearing Gameobject (Vuforia)

Hi guys,
Im creating an Augmented Reality app with Vuforia and Unity. I’d like to be able to display text via Image Target (a picture that gets recognized which then projects a 3d model for example over it), which then dissappears after a set amount of time (say 5 secs). After those 5 secs I want a second 3D model of Text to appear (meaning it had been hidden before) which can than be read by the user. If the Image Target (the tracked picture) is lost, the first Text model should appear again, meaning the script runs newly, sort of a reset (correct me if I’m wrong).
I’m fairly new to programming but I’m in the process of learning C# so any help is useful. I’ve done quite some research and ran some tests, but I haven’t found anything that worked fully yet.
Here’s a script I created based on infos from the web which I then attached to the 3D model which is a child object of the Image Target, but unfortunately it doesn’t work either. If someone could tell me what it wrong with this script, or could provide me with infos for a new and better one I’d be very grateful.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class CustomEvent4KEN : MonoBehaviour

{
	public float targetTime = 10.0f;
	private GameObject KEN;

	// Use this for initialization
	void Start ()
	{
		KEN = GameObject.Find ("KENlogo");
	
	}
	
	// Update is called once per frame
	void Update ()
	{
		targetTime -= Time.deltaTime;

		if (targetTime <= 0.0f)
			DestroyObject (KEN);
	}
}

Here is a basic way of doing your idea:

public class ModelControl:MonoBehaviour
{
     [SerializeField] private GameObject firstObj;
     [SerializeField] private GameObject secondObj;
     private IEnumerator coroutine = null;
     // This is called from the Tracking script, the one that receives the event from Vuforia
     public void EnableItem(){
             if(coroutine != null){ return; }
             coroutine = ShowItem();
             StartCoroutine(coroutine);
     }
     private IEnumerator ShowItem(){
           firstObj.SetActive(true);
          yield return new WaitForSeconds(5f);
          firstObj.SetActive(false);
          secondObj.SetActive(true);
     }
     // This is called from the tracking script when losing track
     public void DisableItem(){
           StopCoroutine(coroutine);
           coroutine = null;
           firstObj.SetActive(false);
           secondObj.SetActive(false);
     }  
}

Quite straightforward. When marker is found, your Tracking script has this OnTrackingFound. So either you connect to this script above or better you had an event. As you mention you are beginning, maybe you are not familiar with events. So just add:

void OnTrackFound(SomeParameterIdontRemember p)
{
      FindObjectOfType<ModelControl>().EnableItem();
} 
void OnTrackLost(SomeParameterIdontRemember p)
{
      FindObjectOfType<ModelControl>().DisableItem();
} 

EnableItem first sets a coroutine, this is meant to avoid multiple calls of the coroutine. Then in the coroutine, it sets the first object and waits for 5 seconds. Then you get it.

If the lost method is called, it stops the coroutine and resets all objects to false.

Simple.

Hello @HcKide !

You have 2 solutions for this (i recommend the number 2).

The first is create a timer (seconds) using Time.deltatime, and check when it reaches a specific time to execute a new order. Try to learn thi by yourself, but if need help to do this, ask again and i will explain you how to create a timer!

The second way, is to use the Invoke method, which allows to execute a method in the same script after determinate time (seconds). This post explains how to do it.

If this helps, Upvote and mark the answer !

If need more help, just ask using @tormentoarmagedoom

Bye :smiley: !