//I have been trying this script, using this the box spawn and disappears on click but the time condition is not fulfilled, it does not spawn after 1 minutes.
using System.Diagnostics;
using System.Threading;
using UnityEngine.XR.ARSubsystems;
public class Anime : MonoBehaviour
{
List<ARRaycastHit> hits;
System.DateTime oldDate; //the time that the game started
System.DateTime currentDate; //the time since the start of the game
int minutes; //the time passed in minutes
public GameObject arobject; //character: box
public ARRaycastManager raycastManager;
void Start()
{
minutes = 0; //the number of minutes we have been on is 0
oldDate = System.DateTime.Now; //the oldDate is the time at the start of the game
arobject.SetActive(true); //box show at start
}
void Update()
{
if (Input.touchCount > 0) // we touched the screen
{
Touch touch0 = Input.GetTouch(0); //first touch in frame
List<ARRaycastHit> allHits = new List<ARRaycastHit>();
if (touch0.phase == TouchPhase.Began)
{
if (raycastManager.Raycast(touch0.position, allHits, TrackableType.Planes)) // if raycast is detecting a plane
{
arobject.transform.position = allHits[0].pose.position;
arobject.SetActive(false); //box disappears
}
}
}
currentDate = System.DateTime.Now; //the current time is now
minutes = currentDate.Minute - oldDate.Minute; //the number of minutes passed is equal to the time right now minus the time the game was started
if(minutes > 1){
arobject.SetActive(true);
}
}
}
If the 60 second delay is fixed, I would suggest creating the WaitForSeconds object once in Start and reusing it to avoid creating a new one every time.
There is also Invoke(string methodName, float delay); which calls a method by name after a given amount of seconds, but I don’t like using it since it relies on the method name, meaning it’s not rename-proof (unless you use nameof(MethodName) instead of a string literal) and uses Reflection.
By the way, I don’t know if that is why your current solution failed, but it would (also) not work if the current time you store in oldDate is one minute before a full hour, because then you would never get minutes to be > 1, since DateTime.Minute is always in the [0…59] range.
EDIT: sorry, rereading the script I’m not sure I correctly understood what you’re after. If you want the object to (re-)spawn one minute after deactivating it by clicking on it, you would move the StartCoroutine call into the if-clause in line 32.
Hey, thanks for replying, I tried this, but this method is not working for building AR application, can you please suggest some other method!! I have tried inbuilt stopwatch from System.diagnostics, I have tried implementing a stopwatch that’s not working as well, is there something else I can do!
I am sharing the code, please ignore the comments:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR.ARFoundation;
using UnityEngine.EventSystems;
using System.Diagnostics;
using System.Threading;
using UnityEngine.XR.ARSubsystems;
public class Anime : MonoBehaviour
{
List<ARRaycastHit> hits;
public GameObject arobject; //character: box
public ARRaycastManager raycastManager;
// public bool stopSpawning = false;
void Start()
{
// minutes = 0; //the number of minutes we have been on is 0
// oldDate = System.DateTime.Now; //the oldDate is the time at the start of the game
arobject.SetActive(true); //box show at start
// StartCoroutine(SpawnAfterSeconds(60));
//Invoke("SpawnObject", 10.0f);
}
// public void SpawnObject() {
// Instantiate(arobject, transform.position, transform.rotation);
// if(stopSpawning) {
// CancelInvoke("SpawnObject");
// }
// }
void Update()
{
if (Input.touchCount > 0) // we touched the screen
{
Touch touch0 = Input.GetTouch(0); //first touch in frame
List<ARRaycastHit> allHits = new List<ARRaycastHit>();
if (touch0.phase == TouchPhase.Began)
{
if (raycastManager.Raycast(touch0.position, allHits, TrackableType.Planes)) // if raycast is detecting a plane
{
arobject.transform.position = allHits[0].pose.position;
arobject.SetActive(false); //box disappears
StartCoroutine(SpawnAfterSeconds(60.0f));
//arobject.SetActive(true);
// private void Start()
// {
// StartCoroutine(SpawnAfterSeconds(60));
// }
}
}
}
// currentDate = System.DateTime.Now; //the current time is now
// minutes = currentDate.Minute - oldDate.Minute; //the number of minutes passed is equal to the time right now minus the time the game was started
// if(minutes > 1){
// arobject.SetActive(true);
// }
}
IEnumerator SpawnAfterSeconds(float seconds)
{
yield return new WaitForSeconds(seconds);
// spawn object
arobject.SetActive(true);
}
}