Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
1
Question by Tyler 2 · Dec 12, 2010 at 09:04 AM · instantiateenabletimed

Timed intervals?

Hello. I know how to enable objects, but how can I make it so that an object is enabled at the next set time interval? For example: If the time interval is every 5 seconds and the "object enabled" condition is met by the player at 13 seconds (since the start of the scene), then the object will be enabled at 15 seconds. If the player meets the condition at 21 seconds, it will be enabled at 25 seconds and so on. How can I do this?

I am going to have one object run from the start and then the second object will be enabled by the player at some point, but I want them both to be synced up...both object fire projectiles every .3 seconds if that helps put things into context.

Thanks.

EDIT- How would I incorporate something like that into this script?

static var MainLvl = 0; var Upgrade1 : GameObject; var Upgrade2 : GameObject; var Upgrade3 : GameObject;

function Update (){ if (MainLvl >0) {Upgrade1.active = true;} if (MainLvl >1) {Upgrade2.active = true;} if (MainLvl >2) {Upgrade3.active = true;} }

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

· Add your reply
  • Sort: 
avatar image
3

Answer by Statement · Dec 12, 2010 at 12:41 PM

Use InvokeRepeating to test if a condition is met and apply a response.

// This is the condition that the player will set. public var condition : boolean;

function Start() { // Calls TestCondition after 0 seconds // and repeats every 5 seconds. InvokeRepeating ("TestCondition", 0, 5); }

// Because of InvokeRepeating, tis is called every 5 seconds. function TestCondition() { if (condition) { // Condition is met, cancel the repeating invoke // and call OnCondition, which handle the details // for what to do when the condition is true. CancelInvoke("TestCondition"); OnCondition(); } }

// This is called if the condition is met every 5 seconds. function OnCondition() { // Enable that object that you wanted to enable. print("Condition met!"); }

By the way, I am not sure what you're trying to achieve but it sounds that the example on the documentation page seems related;

// Starting in 2 seconds. // a projectile will be launched every 0.3 seconds

var projectile : Rigidbody;

InvokeRepeating("LaunchProjectile", 2, 0.3);

function LaunchProjectile () { var instance : Rigidbody = Instantiate(projectile); instance.velocity = Random.insideUnitSphere * 5; }

Comment
Add comment · Show 3 · 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 Tyler 2 · Dec 13, 2010 at 05:01 AM 0
Share

Thank you for your help. Is there any way I can use that with multiple if conditions doing different things, without having to have a separate "TestCondition" and "OnCondition" functions? $$anonymous$$y actual script controls many different objects, and if I have that many functions it is going to get very complex and unorganized.

avatar image Statement · Dec 15, 2010 at 01:21 AM 0
Share

Do you mean that every 5 seconds, it should test a lot of different conditions and for each condition that is met, something special should happen?

avatar image thunderbuns · Apr 05, 2018 at 08:28 PM 0
Share

Can I have the c# version of this?

avatar image
0

Answer by azzogat · Dec 12, 2010 at 12:20 PM

Just store a variable that holds the time and then do a check for variable + 5.0 seconds.

1 (not tested):

var enabledTime: float; var nextEvent: float = 5.0;

// set initial time if(enabledCondition) { enabledTime = Time.time; }

//check timer

if(Time.time == enabledTime+nextEvent) { //do something }

Comment
Add comment · Show 2 · 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 Tyler 2 · Dec 12, 2010 at 07:05 PM 0
Share

How would I incorporate that into the script in my edited question?

avatar image azzogat · Dec 12, 2010 at 08:34 PM 0
Share

Statement's response is a much more efficient way to address this (that's why I voted it up).

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.

Follow this Question

Answers Answers and Comments

1 Person is following this question.

avatar image

Related Questions

Do empty transforms take up much CPU power? 1 Answer

[solved]how can i enable or disable a component of instantiated objects? 5 Answers

Checking if object intersects? 1 Answer

Select an enemy to attack with buttons? 2 Answers

Enable or Instantiate 3 Answers


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