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 chucky831 · Jun 10, 2013 at 11:25 AM · instantiatecoroutineclickwhile loop

instantiate object infinitely

I have a script that instantiates an object infinitely every second. How come every so often there is a little click?

 #pragma strict
 var speed : int = 2;
 var chrismasBall : GameObject; //GameObject
 var instanza:GameObject[]; //instanza
 //var texBall : Texture[] = new Texture [5]; //Array di texture
 
 var score : int = 0;
 
 
 
 function Start () {
 
 StartCoroutine(Creation(1)); //Coroutine
 
 
 
 
 
 }
 
 
 
 function Update () {
 //Physics.gravity = Vector3(0, -0.05, 0); // aggiungo gravità
 
 /*if(instanza==null){
 
 print("cancello");
 }
 */
 
 }
 
 
 
 
 
 
 
 function Creation(tempo){
 
 while(true){ //finchè è vero esegui il ciclo
    
        var posizione : Vector3 = Vector3 (Random.Range(-1.9,1.9),2.5,2);
       var pallaNuova : int =  Random.Range(0,instanza.length-1);
       Instantiate (instanza[pallaNuova], transform.position+posizione, Random.rotation); //instanza chrismasBall
      
      
       
      //instanza.AddComponent(Rigidbody); //aggiungo rigidbody alle instanze
      
       /*
       instanza.transform.position.x = Random.Range(2,-2); //raggio di instanza
       instanza.transform.position.y = 2; 
       instanza.transform.position.z = 0.2; //distanza dalla Main Camera
       instanza.transform.Translate(0, Time.deltaTime, 0); 
       */
       yield WaitForSeconds(tempo);
    
    
  
   
 
 }
 
 
 
 
 
 }





Comment
Add comment · Show 4
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 Dave-Carlile · Jun 10, 2013 at 11:36 AM 1
Share

What do you mean by "a little click"?

avatar image chucky831 · Jun 10, 2013 at 11:46 AM 0
Share

When instantiates the object stops for a few moments, the game slowing down

avatar image ahaykal · Jun 10, 2013 at 12:15 PM 1
Share

why would u want to instantiate infinitely? Ofcourse the game will slow down with soo many objects... At least destroy some objects or something?

avatar image AmoralAckbar · Jun 10, 2013 at 03:38 PM 1
Share

As others have commented, this is not a good idea. If you keep making game objects, you will eventually run out of memory and crash. You are likely going to want to implement some sort of object pooling solution (where you only use a smaller pool of actual objects to simulate a ton of objects). As you are instantiating new objects, you pull from the pool (assu$$anonymous$$g that you don't need some of the old objects anymore and can free them up). Here is something to get you started: Forum thread on object pooling script

0 Replies

· Add your reply
  • Sort: 

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

17 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

Related Questions

Coroutines insider another coroutine. 1 Answer

NullReferenceException: I think unity bug 3 Answers

instantiating at a random time 1 Answer

Mouse Click not detected 1 Answer

Can't get while loop to execute more than once in a coroutine before yielding [Solved] 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