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 Ful · Jan 14, 2014 at 03:37 PM · instantiatespawndelayinvokerepeatingfunction update

How to Instantiate prefabs with function Update?

Hello everyone, I'm developing a 2D shoot-em-up android game where the player controls a spaceship and must avoid incoming meteoroids as long as possible. I manage to get all the core mechanics completed (movement and enemy spawning) and now currently working on some power-ups.

I'm working on implementing a slow down/slow motion power up that effects the gravity scale of the enemy, and so I've decided to re-work my spawning script.

 var delay : float;
 var enemy : GameObject;
 var rightSpawn : float;
 var leftSpawn : float;
 
 function Start () {
 
     InvokeRepeating ("Spawn",delay,delay);
 
 }
 
 function Spawn () {
 
     yield WaitForSeconds (delay);
 
     Instantiate (enemy, new Vector3 (Random.Range (rightSpawn,leftSpawn),10,0), Quaternion.identity);    
     
 }

As you can see above I'm using the InvokeReating to instantiate meteroids (enemy) every 0.27 seconds (delay).

I know I could just make another series of script that gets called using a Time.deltatime to achieve my goal, but if I could somehow manage to change or convert this script to an update function, changing the float variables or any other components such as the gravity will be must more easier.

Therefore I was wondering if anyone has an idea on how I could possibly achieve this?

Comment
Add comment · Show 1
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 craigaw · Jan 17, 2014 at 05:49 AM 0
Share

I'm not sure what you want here? What do you mean by 'change or convert this script to an update function'? Also, there are no variables to control gravity or anything else here, only a line to spawn an enemy at some position every 0.27s.

1 Reply

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

Answer by robertbu · Jan 17, 2014 at 06:06 AM

First your use of 'yield' in your current Spawn() function is not necessary. As for an Update() version of this code, consider using a timestamp. You set a timestamp to some future time. When Time.time is >= to your timestamp, you trigger the event and then recalculate for a future event:

 #pragma strict
 
 var delay : float;
 var enemy : GameObject;
 var rightSpawn : float;
 var leftSpawn : float;
 
 private var timestamp = 0.0;
 
 function Update () {
     if (timestamp <= Time.time) {
         timestamp = Time.time + delay + (Time.time - timestamp);
         Instantiate (enemy, new Vector3 (Random.Range (rightSpawn,leftSpawn),10,0), Quaternion.identity);  
     }
 }

 
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 Ful · Jan 18, 2014 at 01:43 PM 0
Share

Hey, thank you, this works perfectly and finally I can continue on developing my game!

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

19 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

Related Questions

Spawned Enemies: How to Destroy child objects inside their own attached scripts without Destroying the parent object for InvokeRepeating?? 1 Answer

Repeating after variable delay 1 Answer

How to stop instantiate spawn ? 1 Answer

Prevent object from spawning at a spawn point twice in a row? 3 Answers

Ontriggerenter and InvokeRepeating problem 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