Search Unity

Limiting the number of clone objects

Discussion in 'Scripting' started by yonder87, Apr 13, 2013.

  1. yonder87

    yonder87

    Joined:
    Aug 2, 2012
    Posts:
    36
    Hi!!

    How do I set the number of objects in the enemy clone?

    thanks :)

    Code this:

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class EnemyStart : MonoBehaviour {
    5.    
    6.    
    7.     public Transform enemy;
    8.    
    9.  
    10. void Update () {
    11.        
    12.        
    13.         enemy.transform.position = new Vector3(Random.Range(-12.0f,10.0f),0,0);
    14.        
    15.         Instantiate(enemy,enemy.position,transform.rotation);
    16.  
    17.     }
    18. }
    $game.jpg
     
    Last edited: Apr 13, 2013
  2. GuitarJonas

    GuitarJonas

    Joined:
    Sep 28, 2011
    Posts:
    226
  3. Viking1972

    Viking1972

    Joined:
    Jul 24, 2009
    Posts:
    74
    Hi

    The first idea that comes to mind would be to wrap the Instatiate code in an if-statement and create an enemyCout variable where you preset how many enemies you want, then check the count in the if-satement to see if the count is less than or equal to the value in enemyCount.

    Whether this would be the correct way of doing it, I don't know, but as I wrote earlier this is the first idea that come to my mind.
     
  4. GuitarJonas

    GuitarJonas

    Joined:
    Sep 28, 2011
    Posts:
    226
    I believe that a loop would be the easiest way actually. Have it check if the number of clones = to max number of mobs, if it is not, instantiate some more ;)
     
  5. yonder87

    yonder87

    Joined:
    Aug 2, 2012
    Posts:
    36
    thanks friends!! :p happening with the for loop..

    Code (csharp):
    1.  
    2. public Transform enemy;
    3. public int enemyCount = 100;
    4.    
    5.     void Start(){
    6.        
    7.         StartCoroutine(method());
    8.  
    9.     }
    10.  
    11.    
    12.     void Update () {
    13.  
    14.  
    15.     }
    16.    
    17.     IEnumerator method(){
    18.        
    19.        for(int a = 0; a < enemyCount; a++){
    20.        
    21.            yield return new WaitForSeconds(1);
    22.  
    23.            Instantiate(enemy,enemy.position,transform.rotation);
    24.            
    25.            enemy.transform.position = new Vector3(Random.Range(-12.0f,10.0f),0,0);
    26.  
    27.         }
    28.            
    29.            
    30.     }
     

    Attached Files:

    Last edited: Apr 13, 2013
  6. GuitarJonas

    GuitarJonas

    Joined:
    Sep 28, 2011
    Posts:
    226
    Good thing that its working. Good luck with the game.

    -Jonas Rasmussen