Search Unity

Instantiating Multiple and Random Prefabs with an Explosion? Achieved but not ideal result.

Discussion in 'Scripting' started by VAN-D00M, Jul 23, 2014.

  1. VAN-D00M

    VAN-D00M

    Joined:
    Dec 24, 2013
    Posts:
    44
    Hi

    I have a top down shooter and my hazards (Asteroids) happily float around. I have 3 different asteroids in 3 different sized which totals to 9. I have 3 different large ones, 3 different medium ones and 3 different small ones, you get the idea.

    I have a large asteroid floating around, when that is shot it instantiates its explosion and then I instantiate 2 medium ones. When they are shot they instantiate 2 small ones. Makes sense, nice affect.

    Problem 1) I can't instantiate more than 2 asteroids without them violently flying across the map, which won't do.

    Problem 2) For testing purposes I have written in a line to instantiate a specific asteroid, but as I have 3 different asteroids of each size, I would like it to randomly pick out of the 3. Not sure the best way to do that, and I know nothing about arrays.

    Concern 1) At the moment I have 2 identical scripts on the large and the medium sized asteroids with a letter difference. The thought of having possibly one script that did all this and validated it with something like if statements blew my mind so its not ideal but it'll work for now?

    The code I am using is below.

    If anyone has any ideas and can help I would very grateful. Cheers :)

    Code (CSharp):
    1. public class LargeAsteroidController : MonoBehaviour
    2. {
    3.  
    4.  
    5.     public GameObject mAsteroid1; //asteroid to be instantiated
    6.  
    7.     public GameObject explosion;
    8.     public GameObject playerExplosion;
    9.  
    10.    
    11.     void OnTriggerEnter (Collider other)
    12.     {
    13.         if (other.tag == "Boundary" || other.tag == "Enemy")
    14.         {
    15.             return;
    16.         }
    17.        
    18.         if (explosion != null)
    19.         {
    20.             Instantiate(explosion, transform.position, transform.rotation);
    21.         }
    22.        
    23.         if (other.tag == "Player")
    24.         {
    25.             Instantiate(playerExplosion, other.transform.position, other.transform.rotation);
    26.             //gameController.GameOver();
    27.         }
    28.        
    29.         Destroy (other.gameObject);
    30.         Destroy (gameObject);
    31.  
    32.        
    33.         Instantiate(mAsteroid1, transform.position, transform.rotation);
    34.         Instantiate(mAsteroid1, transform.position, transform.rotation);
    35.  
    36.     }
    37. }
    38.