Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Nov 01, 2017 at 01:39 PM by HcKide for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by HcKide · Sep 07, 2017 at 06:18 PM · scripting problemgameobjecttimervuforiadestroygameobject

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);
     }
 }
 

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

  • Sort: 
avatar image
2
Best Answer

Answer by fafase · Sep 15, 2017 at 07:31 PM

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Golnahali · Feb 17, 2019 at 01:58 PM 0
Share

what is "SomeParameterIdontRemember p"?

avatar image
1

Answer by tormentoarmagedoom · Sep 15, 2017 at 11:04 AM

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 :D !

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Follow this Question

Answers Answers and Comments

119 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Learning Unity : GameObject Activation/DeActivation & Calling Components of child of another GameObject 0 Answers

Where is the problem with the script, i want my player to die when his health is <1 1 Answer

How to detect an object which be in FOV of certain camera ? 1 Answer

GameObject is already being activated or desactivated 2 Answers

Loading AssetBundles from inside the Project 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges