Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
2
Question by MikoDee · Dec 07, 2015 at 09:27 AM · c#instantiateprefabstime.deltatime

How to instantiate prefabs at certain times?

Hello guys!

I'm trying to make a mini Guitar Hero clone for a project in school. I am new to Unity and lack many knowledge and coding skills. Right now I am struggling to instantiate prefabs (which would be the "notes" or "frets" of the game) at certain seconds.

For example, I have a 30 second song and would like to instantiate prefabs at the seconds 3, 5, 9, etc.

I came up with a very basic and primitive code by making a timer and instantiating from an array of prefabs when the timer is a certain value, like this:

 float timer;    
 
 void Update () {
 
         timer += Time.deltaTime;
 
         if (timer == 5)
             Instantiate (Notes [blue], BlueSpawn.position, Quaternion.identity);
         if (timer == 10)
             Instantiate (Notes [red], RedSpawn.position, Quaternion.identity);

But well, I would end up making a wall of "if" and the code doesn't instantiate anything at all. Is there a better way of generating prefabs at certain points in time? 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
7
Best Answer

Answer by wibble82 · Dec 07, 2015 at 10:12 AM

Hi there

The first problem you're hitting is that 'timer' is a floating point number - i.e. it can contain fractional values. As a result, even if the maths looks like timer should eventually equal 5 or 10, its unlikely to ever be exactly 5 or 10. So if Time.deltaTime is 1/60, you might expect that after 60 frames time is 1, but it might actually be 0.99999999 or 1.00000001. For this reason, precise comparisons of floating point numbers are rarely useful. Normally to see if a floating point value is equal to something, your code would look more like:

 //if timer is 'almost exactly'' 5 - i.e. more than 4.99999 or less than 5.00001
 if(timer >= 4.99999 || timer <= 5.00001)

Though a better way to write it might be:

 //a cleaner way of writing it - if the absolute (i.e. positive) value given 
 //by timer-5 is less than 0.00001
 if(Mathf.Abs(timer-5.0f) < 0.000001f)  

However, for your purposes a simple comparison is probably not what you want, as Time.deltaTime varies and is big enough that you might skip over 5 (or any of the other numbers) altogether! For example, timer might be 4.9 one frame, then 5.1 the next - skipping 5 altogether!

A more common way of writing your logic would be to record a bit of extra information - either the next time we're going to spawn, or the previous time we spawned. In addition, we might use the Time.time value rather than Time.deltaTime, to avoid inacuracies in our time tracking:

 float next_spawn_time;
 
 void Start()
 {
     //start off with next spawn time being 'in 5 seconds'
     next_spawn_time = Time.time+5.0f;
 }
 
 void Update()
 {
     if(Time.time > next_spawn_time)
     {
         //do stuff here (like instantiate)
         Instantiate(bla bla bla)
 
         //increment next_spawn_time
         next_spawn_time += 5.0f;
     }
 }

This approach has the advantages:

  • It never 'misses' the time to spawn, as its only ever checking if 5s have passed since the last spawn

  • It works no matter how much time has passed

  • It isn't dependent on comparing numbers to each other

  • It doesn't get increasingly innacurate, as its using the accurate Time.time value

It's only disadvantage is that you are reliant on Time.time, so if you wanted your timing to be independent (say for example you only wanted to 'increment' time when the player was holding down a button, it wouldn't work. In this case you'd write very similar code, however you'd keep track of the current 'timer' yourself - just like in your first example.

Hope that helps and things are a bit clearer now.

Tip: If you want to start dealing with much smaller time steps (ones that might be closer to Time.deltaTime itself), you'd need to get a bit cleverer and use a while loop, potentially spawning multiple objects per frame. I'll leave that to you though if you do eventually need it :)

-Chris

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 MikoDee · Dec 08, 2015 at 06:18 AM 0
Share

Thank you! This worked for me. I also used an array of float numbers to deter$$anonymous$$e the seconds in which they will appear, combined with your code they totally work now! Thank you Chris!

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Instantiate prefab from folder, not scene 0 Answers

How can I in-script create gameobject from prefab? 2 Answers

Destroy a Prefab from an Array? (C#) 2 Answers

Instantiation of my GameObjects spell (from other script) 0 Answers

Instantiated Prefab won't instantiate with a script reference 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