Random generation of random asteroids

Hi all,

I need to write a script to populate a scene with randomly generated asteroids for a 3rd person 3D game. This script should be able to choose from 6 models and add various components like rigidbody, collider and force. It should also assign random size/mass values and random force values (both within a range). I will reuse/modify this script to place power-ups in the scene.

I know basic C# scripting and I’m willing to learn. I use Unity2017/C# and know very little of Java.

Thanks for any insight/code.

Here is the basic concept:

using UnityEngine;

public class AsteroidHandler : MonoBehaviour
{
    [SerializeField]
    private GameObject[] AsteroidPrefabs;

    void Start()
    {
         SpawnAsteroids(100);
    }
    
    private void SpawnAsteroids(int amount) 
    {
         for (int i=0; i < amount; i++) 
         {
              var pos = new Vector3(Random.Range(-150, 150), Random.Range(-150, 150), Random.Range(-150, 150));
              var chosenAsteroid = AsteroidPrefabs[Random.Range(0, AsteroidPrefabs.Length)];
              var asteroid = Instantiate(chosenAsteroid , pos, Quaternion.identity);

              // You can still manipulate asteroid afterwards like .AddComponent etc
         }
    }
}

Here’s the code I’ve written to achieve this goal. I’ve been revising this post as I’ve added key elements. Namely, it now does assign random size, mass and force to the asteroids. The trick was apparently to make my instantiated prefabs also GameObjects and then to use arrays to modify components values of each clone.

The values chosen suit my project but you can change them for yours. If you see anything that could be improved, let me know.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Asteroides : MonoBehaviour {

    // Instantiates prefabs in boxed terrain.

    public GameObject[] Asteroid;
    private int nombreDobjets;
    private int index;
    private Rigidbody[] rb;
    private ConstantForce[] cf;

    void Start()
    {
        nombreDobjets = Random.Range(888, 1234);
        Vector3 TailleTerrain = GameObject.Find("Terrain").GetComponent<Terrain>().terrainData.size;
        rb = new Rigidbody[nombreDobjets];
        cf = new ConstantForce[nombreDobjets];

        for (int i = 0; i < nombreDobjets; i++)
        {
            index = Random.Range(0, 6);

            Vector3 pos = new Vector3(Random.Range(0, TailleTerrain.x), Random.Range(0, TailleTerrain.y), Random.Range(0, TailleTerrain.z));

            GameObject go = Instantiate(Asteroid[index], pos, Quaternion.identity) as GameObject;

            go.AddComponent<Rigidbody>();
            go.AddComponent<CapsuleCollider>();
            go.AddComponent<ConstantForce>();
            go.transform.localScale += new Vector3(Random.Range(0, 2f), Random.Range(0, 2f), Random.Range(0, 2f));

            rb *= go.GetComponent<Rigidbody>();*

cf = go.GetComponent();
rb*.mass = Random.Range(345, 1000);*
rb*.AddForce(Random.Range(0, 23), Random.Range(0, 23), 0, ForceMode.Acceleration);*
rb*.AddTorque(Random.Range(0, 23), Random.Range(0, 23), Random.Range(0, 23), ForceMode.Acceleration);*
cf*.force = new Vector3(0,188,0);*
}
}