- Home /
Random spawning, but one definite object
Hi, I am new to coding so I don't know if this is a basic question or not. I would like a code that picks randomly 1 object from a list of 3 and spawns that object at each of my 5 spawn points. Here is the thing I don't know how to do; I want it to always spawn 1 specific object everytime at 1 random spawn point of the 5. How would I go about doing this?
@FatalNightmare ... how about marking the correct answer if this helped you with your coding?
Answer by DiGiaCom-Tech · Mar 18, 2015 at 01:59 AM
Basically you'll need an array of GameObjects to be spawned and an array of Vector3 locations where these objects could be spawned to. Once these are populated you then randomly select an object and and then randomly select a location. Once this is done you instantiate the object at the selected location. Something like this perhaps...
public GameObject[] MyObjects;
public Vector3[] MyLocations;
You can then set/populate these in the inspector. Next in your 'Update' method you'll need some sort of timer to trigger the spawning -or- an method that can be called to trigger the spawning. This method will call a function to generate the two random numbers.
int ObjectID = Random.Range(0, 2); // Returns integer from 0 to 2
int LocationID = Random.Range(0, 5); // Returns integer from 0 to 4
Finally you can call the instantiate method to make it happen...
GameObject SpawnedObject = (GameObject)Instantiate(MyObjects[ObjectID], MyLocations[LocationID ], Quaternion.identity);
Those are the basics ... hope this helps ;)
UPDATE
Ok ... looks like I made one typo on the randomization code (should have been 3 not 2) but here is a script for you to try ;)
It has both a timed spawn and a triggered spawn (e.g. a GUI button). Note that the Instantiate code is returning the spawned object into a GameObject variable called SpawnedObject so you can alter it after it's spawned (e.g. re-name/re-tag, change scale/rotation/materials, etc.).
using UnityEngine;
using System.Collections;
public class Spawner : MonoBehaviour
{
public GameObject[] MyObjects;
public Transform[] MyLocations;
public float MyTimer = 5f;
private float CurTime;
// Use this for initialization
void Start () {
// Initialize the timer value
CurTime = MyTimer;
}
// Update is called once per frame
void Update () {
// Decrement timer value
CurTime -= Time.deltaTime;
// If time has run out
if (CurTime <= 0f)
{
// Call the spawner
SpawnTrigger();
// Reset the timer value
CurTime = MyTimer;
}
}
void OnGUI()
{
//If they click the button
if (GUI.Button(new Rect(Screen.width / 2f - 50f, Screen.height * 0.1f, 100f, 20f), "Spawn Now"))
{
// Call the spawner
SpawnTrigger();
// Reset the timer value
CurTime = MyTimer;
}
}
void SpawnTrigger()
{
int ObjectID = Random.Range(0, 3); // Returns integer from 0 to 2
int LocationID = Random.Range(0, 5); // Returns integer from 0 to 4
GameObject SpawnedObject = (GameObject)Instantiate(MyObjects[ObjectID], MyLocations[LocationID].position + Vector3.up * 25f, Quaternion.identity);
}
}
Enjoy ;)
Your answer
Follow this Question
Related Questions
Instantiate random object from Array 2 Answers
I want to swap randomly 5 blocks (block has 5 diffrent color) and spawn in vertical line 1 Answer
Weighted Random Spawning 1 Answer
Enemies spawn on top of each other(C#)(Unity) 1 Answer
How do I make a game object spawn several times in semi-random locations? 1 Answer