• 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 /
avatar image
0
Question by velarde101 · Mar 20, 2014 at 10:19 PM · delayseconds

How Do I delay the time in which my object turns active?

How Do I delay the time in which my object turns active? I've already looked into

 void return new WaitForSeconds()

Maybe I'm not appling it correctly within my code? The following is the code which triggers an object to switch to active after player has entered collider.

 enter code hereusing UnityEngine;
 
 public class ActivateTrigger : MonoBehaviour {
     
     public enum Mode {
         Trigger   = 0, // Just broadcast the action on to the target
         Replace   = 1, // replace target with source
         Activate  = 2, // Activate the target GameObject
         Enable    = 3, // Enable a component
         Animate   = 4, // Start animation on target
         Deactivate= 5 // Decativate target GameObject
     }
     
     
     /// The action to accomplish
     public Mode action = Mode.Activate;
 
     /// The game object to affect. If none, the trigger work on this game object
     public Object target;
     public GameObject source;
     public int triggerCount = 1;///
     public bool repeatTrigger = false;
     
     void DoActivateTrigger () {
         triggerCount--;
         
 
         if (triggerCount == 0 || repeatTrigger) {
             Object currentTarget = target != null ? target : gameObject;
             Behaviour targetBehaviour = currentTarget as Behaviour;
             GameObject targetGameObject = currentTarget as GameObject;
             if (targetBehaviour != null)
                 targetGameObject = targetBehaviour.gameObject;
         
             switch (action) {
                 case Mode.Trigger:
                     targetGameObject.BroadcastMessage ("DoActivateTrigger");
                     break;
                 case Mode.Replace:
                     if (source != null) {
                         Object.Instantiate (source, targetGameObject.transform.position, targetGameObject.transform.rotation);
                         DestroyObject (targetGameObject);
                     }
                     break;
                 case Mode.Activate:
                     targetGameObject.active = true;
                     break;
                 case Mode.Enable:
                     if (targetBehaviour != null)
                         targetBehaviour.enabled = true;
                     break;    
                 case Mode.Animate:
                     targetGameObject.animation.Play ();
                     break;    
                 case Mode.Deactivate:
                     targetGameObject.active = false;
                     break;
             }
         }
     }
 
     void OnTriggerEnter (Collider other) 
     {
         DoActivateTrigger ();
     }
 }
Comment
Add comment · Show 7
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 Starwalker · Mar 20, 2014 at 10:48 PM 0
Share

Not tested, but try this:

In the switch case:

 case $$anonymous$$ode.Activate:
               StartCoroutine("Call$$anonymous$$e$$anonymous$$aybe"); // wait 5 seconds, and continue operation as usual.
               targetGameObject.active = true;
               break;


//Outside the DoActivateTrigger () function.

   int waitTime = 5;
      IEnumerator Call$$anonymous$$e$$anonymous$$aybe() {
             
             yield return new WaitForSeconds(time);
            
         }
avatar image robertbu · Mar 20, 2014 at 10:52 PM 0
Share

@Starwalker - this will not work as is. Run a quick test, and you will see that the switch statement will execute immediately.

avatar image FinKone · Mar 21, 2014 at 03:29 AM 1
Share

StartCoroutine(NameYourIEnumerator()); //call the IEnumerator in trigger.

//setup the coroutine youll be calling outside of update.

IEnumerator NameYourIEnumerator() { return yield new WaitForSeconds(5) //do stuff after 5 seconds. }

$$anonymous$$ight have new / yield backwards... on my phone so I didnt use code box.

avatar image velarde101 · Mar 21, 2014 at 06:02 PM 0
Share

Thank you for your responses! No Dice! I get error: 'IEnumerator' couold not be found. $$anonymous$$issing a using directive or an assemble reference?

avatar image Starwalker · Mar 21, 2014 at 07:12 PM 0
Share

Can you test the code from here in an empty project?

Co-routine on Unity

The reason being that if IEnumerator is not identified by $$anonymous$$ono Compiler then you have bigger problems..

avatar image TropicalTrevor · Mar 21, 2014 at 07:33 PM 0
Share

In C# you'll have to include dot net features with using directives.

All the way at the top of your file put

using System.Collections

To find out which module you're using you can google or search on http://msdn.microsoft.com/en-us/library/system.collections.ienumerator%28v=vs.110%29.aspx and in that page (for whatever function you searched) it will say 'namespace: ' somthing, that something you'll need to be using. This is most often the case of stuff being undefined when copying a script (or you missed part of the copying) ;)

avatar image velarde101 · Mar 24, 2014 at 11:52 PM 0
Share

Yeah....... I have BIGGER problems! Thanks for the help, all!

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by RyanZimmerman87 · Mar 25, 2014 at 12:16 AM

Hmm I've always been able to use StartCoroutine and IEnumerator just fine in C# with the default MonoDevelop set up, didn't need any additional libraries.

Are you using Visual Studio or something?

If you are using MonoDevelop my guess is you just have some simple syntax error which is causing it to not compile.

You should post a copy of your script including your StartCoroutine and IEnumerator work. I'm guessing there's just something about your code that isn't set up quite right.

It should be as easy as this example to get this to work and in my experience this is by far the best way to work with specific delay timings for my project.

 //start coroutine this should only be
 //called once per delay cycle

 StartCoroutine(delayEnumerator(delayTime));

 //IEnumerator performs the delay
 IEnumerator delayEnumerator(float newDelayTime)
 {
 //waits for the seconds sent from the
 //delayTime in StartCoroutine

 yield return new WaitForSeconds(newDelayTime);

 //do the logic which you want to occur 
 //after the delay
 }

You are setting this whole system up a lot differently than anything I've done. I don't ever use switch statements or even public enum, so I can't really comment too much on the other logic but getting a delay with a coroutine should be simple if none of your other logic is interfering.

I'm assuming you want the delay to continue triggering for your DoActivateTrigger() since you only call it once with OnTriggerEnter()?

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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

24 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

Related Questions

Waiting for fractions of a Second 1 Answer

destroy object after a delay? 6 Answers

how can customize latency in Unity? 0 Answers

Canvas position not in sync with camera 0 Answers

How to apply Time.deltaTime here? 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