Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Best Way to Make Thousands of Copies of a Prefab?

Discussion in '2D' started by plzburnthis, May 18, 2014.

  1. plzburnthis

    plzburnthis

    Joined:
    May 15, 2014
    Posts:
    3
    Hello, I started using Unity this weekend for the first time. Actually first time for any game/app stuff ever. And first post! :mrgreen:

    I am making a 2D game that has about 2000 balls which fall down. All are instantiated prefabs with circle colliders, and a script for some scoring. At lower numbers, like 500, this runs fine. At higher numbers like 2000 the game is very laggy on both my PC and Android.


    My Questions Are:
    Is this just too much for today's average hardware/software?
    Should I change my methodology/scripts?

    If the answer is to optimize my methodology/scripts, then here is a more detailed description of my methodology and the associated scripts.


    BallGenerator.js instantiates the balls (and is run from an empty GameObject):

    Code (csharp):
    1. // JavaScript
    2. #pragma strict
    3. var hundoballs : GameObject;
    4. function Start () {
    5.     for (var y = 0; y < 32; y++) {
    6.         for (var x = 0; x < 16; x++) {
    7.             Instantiate(BallGenerator, Vector3 (x-15, y+20, 0)*.6, Quaternion.identity);
    8.         }
    9.     }
    10. }
    The prefab is a small PNG (32px X 32px) and has rigidbody2d, circlecollider2d and BallController.cs for keeping track of the scoring and moving the balls back to the top of the screen.

    BallController.cs
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class BallController : MonoBehaviour {
    5.  
    6.     private Transform spawnPoint;
    7.     public bool respawnTop = false;
    8.  
    9.      
    10.         void OnBecameInvisible()
    11.     {
    12.         if (respawnTop == true)
    13.         {
    14.         // float xMax = Camera.main.orthographicSize - 0.5f;
    15.         // transform.position = new Vector3( Random.Range (-xMax, xMax), spawnPoint.position.y, transform.position.z );
    16.             transform.position = new Vector3(spawnPoint.position.x, spawnPoint.position.y, transform.position.z);
    17.  
    18.             respawnTop = false;
    19.         }  
    20.     }                              
    21.  
    22.     // Use this for initialization
    23.     void Start () {
    24.         spawnPoint = GameObject.Find("SpawnPoint").transform;
    25.    
    26.     }
    27.    
    28.     // Update is called once per frame
    29.     void Update () {
    30.    
    31.     }
    32.  
    33.  
    34.    
    35.     void OnTriggerEnter2D(Collider2D playerScore)
    36.     {
    37.         respawnTop = true;
    38.  
    39.     }
    40.  
    41.  
    42. }
     
  2. Loius

    Loius

    Joined:
    Aug 16, 2012
    Posts:
    546
    Right, you can't have thousands of GameObjects because it's just too expensive.

    Usually the solution is to run a controller script that displays the objects without having thousands of copies - often something that updates a mesh in real time.
     
  3. DaDarkDragon

    DaDarkDragon

    Joined:
    Jun 6, 2013
    Posts:
    115
    You may want to look into object pooling.
     
  4. plzburnthis

    plzburnthis

    Joined:
    May 15, 2014
    Posts:
    3
    Thank you both for your replies. =D


    My idea was that the the objects have a lifetime in which individual's textures will change, it doesn't look like object pooling works well for that(?)


    For anyone else who is interested, here is a resource, with C# ConcurrentBag for object pooling by Microsoft.

    Honestly, I think it might just be a bit much for my first coding project. =P