Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class Example : MonoBehaviour
- {
- public GameObject objectToSpawn; //prefab or gameobject
- public Transform objectContainer; //parent
- public float thingsToSpawn = 5f;
- public float radius = 5f;
- public float min = 0.5f;
- public float max = 1f;
- [Header("Press me to spawn things")]
- public bool toSpawn = false; //flipflop flag
- int objectID = 0; //arrays start from 0
- string objName;
- string objNameBase = "base";
- private GameObject newObject; //Creates a new object.
- void Update()
- {
- if (toSpawn)
- {
- SpawnObjects();
- toSpawn = false;
- }
- }
- public void SpawnObjects() //The function that spawns the objects in the scene.
- {
- for (int i = 0; i < thingsToSpawn; i++)
- {
- Vector2 spawnCircle = Random.insideUnitSphere * radius; //The spawn radius that the object will appear.
- Vector3 spawnPos = new Vector3(spawnCircle.x, 0f, spawnCircle.y); //The spawn position of the object.
- float objectScale = Random.Range(min, max); //Gives a random scale to the object.
- newObject = Instantiate(objectToSpawn, spawnPos, Quaternion.Euler(new Vector3(0, Random.Range(0, 360))), objectContainer); //This is where the object is instantiated with a random rotation.
- objName = objNameBase + objectID.ToString();
- objectID++;
- newObject.name = objName; //This is where the name is applied to the object.
- newObject.transform.localScale = Vector3.one * objectScale; //Sets the scale.
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement