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
0
Question by Bumbaz · Sep 15, 2010 at 04:18 PM · instantiateyielddelay

Delay between each instantiation

Hi guys. I'm kind of new to this. I want cubes to spawn randomly in the game every, let's say, 5 seconds. I have this code so far:

var object:Transform; var x = 12; var z = 12; var y = 5;

function Update () {

 var position = Vector3(Random.Range(-x, x), y, Random.Range(-z, z));
 Instantiate(object, position, Quaternion.identity);

}

I tried using yield WaitForSeconds(5.0) but it messed things up... Thank you in advance.

~Bumbaz

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

3 Replies

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

Answer by AliAzin · Sep 15, 2010 at 04:26 PM

you can't use yield in Update(). you should do it in start(), Awake() or custom functions like this:

function Start(){ DoInstantiate();

} function DoInstantiate(){ while (true){ yield WaitForSeconds(5.0); var position = Vector3(Random.Range(-x, x), y, Random.Range(-z, z)); Instantiate(object, position, Quaternion.identity); }

}

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 Bumbaz · Sep 15, 2010 at 05:22 PM 0
Share

Thank you very much!

avatar image plszhao · Mar 27, 2019 at 08:15 PM 0
Share

Work! Thank you so much!!

avatar image
0

Answer by illinar · Dec 19, 2012 at 08:43 AM

C# code I would recommend:

 public GameObject spawnObject;
 public float xRange;
 public float zRange;
 public float y;
 public float spawnRate;
 
 void Awake ()
 {
     InvokeRepeating("Spawn", 0f, spawnRate);
 }
 
 void Spawn () 
 {
     Instantiate(spawnObject, new Vector3(Random.Range(-xRange, xRange), y, Random.Range(-zRange, zRange)), Quaternion.identity);
 }
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
avatar image
0

Answer by unityart3ds · Dec 19, 2012 at 07:53 PM

ok in java script for unity this is how you get the script to spawn a item and when it gets destroyed to let it spawn agein.

var bullet : GameObject;

var bulletbackup : GameObject;

function Update () { if( Input.GetButton("fire1"))

{bulletbackup = Instantiate(bullet, transform, rotation,))}

}

Now to make a spawn time on a object is like this

var spawntime : float = 1;

var resetnumber : float = 10;

function Update (){Time.deltaTime * ++spawntime;

}

That might help but the best way Ive made a spawn time is by useing the animation tool

But I hope this helps.

Oh and im not for sure on the script with the spawn time but that is done in Java

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.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Delay in Instantiate only runs once 2 Answers

How to delay a respawn? 1 Answer

why does Coroutine 'yield return 0' sometimes take a lot of frames before it continues 1 Answer

Respawn after delay 3 Answers

Delay If statement in Update c# 2 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