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 /
avatar image
0
Question by srudgr8 · Jul 25, 2017 at 03:43 PM · instantiategameobjects

Instantiate a single gameobject at every nth second of a minute for a total of x number of gameobjects

I want to instantiate a single gameobject every 6th second, generating a total of 5 gameobjects per minute. The seconds and minutes are referred from another script called "controller". This is the code i have, but instead of a single object, each time numerous gameobjects are generated. I don't know what is wrong with the script, any pointers would be highly appreciated! Thanks

          public int count=5;
           public int timeinterval=6


            Void Update(){
               firstmin();
                       }


               void firstmin(){
     for (int i = 0; i < count; i++) {
     if (Controller.minutes==1 && Controller.seconds!=0 && Controller.seconds % timeinterval == 0) {
             Vector3 pos = new Vector3 (this.transform.position.x, this.transform.position.y, 
                         this.transform.position.z);
             
             GameObject starinstance;
             starinstance = Instantiate (star, pos, star.transform.rotation);
             
         
         }
     }
         
     }
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by wizbit · Jul 25, 2017 at 03:54 PM

or, use a "CoRoutine", simple enough to create

:)

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 Daemonhahn · Jul 25, 2017 at 04:19 PM 0
Share

Coroutines have an overhead (performance cost), and are generally advised against for newcomers who could cause problems firing off too many of them.

I recommend using a timer like above so that the OP gains an understanding of how the update cycle works, before moving onto IEnumerable types.

avatar image
0

Answer by Daemonhahn · Jul 25, 2017 at 03:45 PM

you chould just do (this is off the top of my head so may contain a couple of syntax errors but the algorithm will be correct).

 int timer = 0;
   int totalObjectCount = 0;
   void Update()
   {
  
  
   if(totalObjectCount > objectLimit)
   {
   return;
   } 
  
   timer += Time.deltaTime;
  
   if(timer >= 6f)
   {
   // Instantiate object here
   // set timer to 0 to start countdown again
   timer = 0f;
   totalObjectCount++;
   
  
   }
   
   
   }

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 Daemonhahn · Jul 25, 2017 at 03:46 PM 0
Share

This will create an object every six seconds.

You set an int called timer which acts as the timer.

You set an int called objectLimit (or just put the number directly) to set when to stop adding objects

avatar image srudgr8 Daemonhahn · Jul 25, 2017 at 04:31 PM 0
Share

Thanks a lot, it works great :)

avatar image Daemonhahn srudgr8 · Jul 25, 2017 at 05:54 PM 0
Share

No problem, if your problem is resolved if you could select my answer as the correct one I would really appreciate it :)

avatar image Daemonhahn · Jul 25, 2017 at 03:47 PM 0
Share

In fact you need to move the total object count part above so its like this:

  int timer = 0;
  int totalObjectCount = 0;
  void Update()
  {
 
 
  if(totalObjectCount > objectLimit)
  {
  return;
  } 
 
  timer += Time.deltaTime;
 
  if(timer >= 6f)
  {
  // Instantiate object here
  // set timer to 0 to start countdown again
  timer = 0f;
  totalObjectCount++;
  
 
  }
  
  
  }
avatar image srudgr8 · Jul 25, 2017 at 04:31 PM 0
Share

Thanks a lot, it works great :)

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

83 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to Instantiate Objects based on a list of integers 0 Answers

Multiple Fire Points 2 Answers

Simple editor script is creating an extra GameObject when using GameObject.Instantiate 1 Answer

I am trying to spawn coloured cubes that match a pixel image. At the moment the cubes spawn but i do not know how to match up the colours. 0 Answers

Instantiating 20K gameobjects performance issue 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