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 ImpactUK · Nov 23, 2010 at 02:15 AM · instantiatecollidertagdelay

Couple of questions regarding instantiate delays

First question: I have a tank that fires a prefab projectile which is instantiated when i click the mouse button. Is there a way that i can delay the instantiation of the projectile. Sort of like a reload time? Current script:

var rocket : Rigidbody; var speed = 400.0;

function BombsAway () { var rocketClone : Rigidbody = Instantiate(rocket, transform.position, transform.rotation); rocketClone.velocity = transform.forward * speed; }

function Update () { if (Input.GetButtonDown("Fire1")) { BombsAway();
} }

Second question: I'm trying to get objectives pop up on the screen for 5 seconds. I've managed to do it using GUI text on the bottom of the screen which instantiates upon going through a collider, then destroys itself after 5 seconds. However, when it instantiates it duplicates itself for everything that goes through it. Is there a way to only instantiate the text if a tagged object goes through? OR is there a way to delay it so it only instantiates once? Heres the code for one of the trigger colliders:

var prefab : GameObject; var delay = 5.0;

function OnTriggerExit () {

Instantiate (prefab); yield WaitForSeconds(delay); }

Sorry if this all seems confusing, i'm not great at explaining what i'm doing. All of this is really new to me! Thanks for all your help in advance

Comment
Add comment · Show 2
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 Uriel_96 · Nov 23, 2010 at 03:20 AM 0
Share

why you dont put yield WaitForSeconds(delay); to the first script"to wait to the tank shoots"

avatar image duck ♦♦ · Nov 23, 2010 at 07:53 AM 0
Share

You can't just add a "yield" to either of these scripts without other modifications. In the 1st script, you can't add yield to an "Update" function. In the 2nd script, the OnTriggerExit will get called for every trigger, whether there's a yield inside it or not.

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by duck · Nov 23, 2010 at 07:52 AM

For the first script, you can either manually track and compare the Time.time value, like this:

var rocket : Rigidbody; var speed = 400.0; var reloadTime = 2.0; private var nextFireTime = 0;

function BombsAway () { var rocketClone : Rigidbody = Instantiate(rocket, pos, rot); rocketClone.velocity = transform.forward * speed; }

function Update () { if (Input.GetButtonDown("Fire1") && Time.time > nextFireTime) { BombsAway(); nextFireTime = Time.time + reloadTime;
} }

Or you could use a coroutine (which basically means a function that makes use of "yield"), like this:

var rocket : Rigidbody; var speed = 400.0; var reloadTime = 2.0;

function Start() { while (true) { if (Input.GetButtonDown("Fire1")) { BombsAway(); yield WaitForSeconds(reloadTime);
} yield; } }

function BombsAway () { var rocketClone : Rigidbody = Instantiate(rocket, pos, rot); rocketClone.velocity = transform.forward * speed; }


To make it so your trigger function only creates the instance once, you could use a boolean var, like this:

var prefab : GameObject; private var created = false;

function OnTriggerExit () { if (!created) { Instantiate (prefab); created = true; } }

Or to make it so only objects with a certain tag trigger it:

var prefab : GameObject;

function OnTriggerExit (c : Collider) { if (c.CompareTag("Player")) { Instantiate (prefab); } }

Or to make it so that it only triggers a maximum of once per N seconds:

var prefab : GameObject; var delay = 5.0; private var waitUntil = 0.0;

function OnTriggerExit () { if (Time.time > waitUntil) { Instantiate (prefab); waitUntil = Time.time + delay; } }

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 ImpactUK · Nov 23, 2010 at 01:00 PM 0
Share

Brilliant! I added your modifications and they work perfectly. I have been struggling for this over the past week, thank you very much.

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

No one has followed this question yet.

Related Questions

Respawn Player Using a Coroutine 0 Answers

How do I acces a specific GameObject within the terrain???? 1 Answer

Network.Destroy and collision; destroying correct object. 1 Answer

Prefab tag bug 0 Answers

Raycast Collider Tag not returning correct result 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