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 Kaltic · Apr 06, 2011 at 08:20 PM · spawntimerspawningincrease

Increasing spawn rate over time

I want to create an enemy that spawn when the level starts and then increase over time. However, with the code I have right now the objects will never actually appear in my game. And if I move the Instantiate to if(spawnTime == 0) then two of each object will spawn (only 1 of each should).

var spawnTime : float = 0; var timeIncrease : int = 0; var Enemy: Transform; var Enemy2:Transform;

function Update(){ if(spawnTime == 0){ timeIncrease = Time.time; spawnTime = Time.time; }

 if(spawnTime == 10){
     Instantiate(Enemy,transform.position,transform.rotation);
     Instantiate(Enemy2,transform.position,transform.rotation);
     spawnTime = 0;
 }

 if(timeIncrease == 30){
     timeIncrease = 0;
     spawnTime = -0.5;
 }

}

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
0
Best Answer

Answer by xCRKx TyPHooN · Apr 06, 2011 at 09:18 PM

SpawnTime is a float and you are comparing it to an int which isn't a good practice. Because spawnTime is a float and is incrementing based on the built in time class, its never going to be exactly equal to 10, therefore, it will never Instantiate your new objects. You could use a typecast to fix this, but its probably best to rewrite your code so you can keep the precision required with time.

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 Kaltic · Apr 06, 2011 at 11:53 PM 0
Share

Well if I modify my code to compare to float times (<= & >=) then it leaves the possibility of it spawning twice very quickly. Is there a way to do it while spawnTime is an integer?

Also is there a way to have the objects spawn right when the level loads, but then not again until 10 seconds later?

avatar image xCRKx TyPHooN · Apr 07, 2011 at 04:58 PM 0
Share

Use a bool and when it hits the spawn timer first at 10 seconds flip a flag so it can't hit it again. You can spawn an object in the Awake() function, as it's called only one.

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

Random Spawning of Objects at Random Locations 1 Answer

How to make an object spawn in different positions? 2 Answers

Object not spawned on future clients 0 Answers

Enemies spawn on top of each other(C#)(Unity) 1 Answer

Using fog/mist to disguise enemy spawning 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