• 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 oliver-jones · Nov 17, 2010 at 10:07 PM · instantiateprefabemptyseconds

Instantiate a Prefab Every 5 Seconds - Help!

Hello,

I know this is a newby question:

Is it possible to instantiate a prefab onto my scene every 5 seconds?

I have an empty in where I would like the prefab to spawn in a certain amount of seconds.

Thanks

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

1 Reply

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

Answer by duck · Nov 17, 2010 at 10:10 PM

Yes you can do this.

Put this script on an empty gameobject, then drag in the 'prefab' reference. The prefab will be instantiated at the empty gameobject's position.

var prefab : GameObject; var timeDelay = 5;

function Start() { while (true) { yield WaitForSeconds(timeDelay); Instantiate( prefab, transform.position, Quaternion.identity ); } }

Comment
Add comment · Show 5 · 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 oliver-jones · Nov 17, 2010 at 10:16 PM 0
Share

Okay, and what would I put if I want to initiate in between random times??

avatar image Peter G · Nov 17, 2010 at 11:30 PM 0
Share

@Oliver Jones: Replace timeDelay with Random.Range() with the parameters you want. On a side note, you could also use InvokeRepeating(). Same effect. Just a different method based entirely on preference.

avatar image duck ♦♦ · Nov 18, 2010 at 12:47 AM 2
Share

As Peter G said. Omit the timeDelay var, and just use: yield WaitForSeconds(Random.Range(1,8)); (example - will wait between 1-8 seconds)

avatar image AgentKnopf · Sep 07, 2015 at 09:29 PM 0
Share

Should this really be done in Start() and not in Update() ? I thought Start() was meant to be used to instantiate things and setup the GameObject/Script ?

avatar image mdraju62 · Aug 22, 2016 at 07:08 AM 0
Share

sir I'm trying to create unlimited time what can i do plz ans

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

1 Person is following this question.

avatar image

Related Questions

Destorying prefab and instanstiating again? 0 Answers

Instantiate PreFab with C# Script 5 Answers

Instantiate A Prefab 1 Answer

Check if object is destroyed on level load, if so instantiate prefab? 1 Answer

Adding a NetworkView component at runtime 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