- Home /
How to random between 2 objects while spawning?
using UnityEngine;
using System.Collections.Generic;
public class Spawner : MonoBehaviour
{
public GameObject[] Monsters;
public Transform[] spawnpoint;
public float time = 0;
void Start ()
{
}
void Update ()
{
time +=
1*Time.deltaTime;
if(time >= 5)
{
time -= 5;
int spawnpointindex = Random.Range (0, spawnpoint.Length);
Instantiate (Monsters[0], spawnpoint[spawnpointindex].position, spawnpoint[spawnpointindex].rotation);
}
}
}
This is my code so far, I'm pretty new to coding so I needed to ask since I can't ask anyone else. Is there a way to random between two objects while spawning continuously and has an option to put allocation on the chances that they can be spawned? For example, 60% for Object A and 40% for Object B
Comment
Answer by gribbly · Nov 08, 2014 at 09:06 PM
Do something like:
float randomChance = Random.Range(0.0f, 1.0f);
if(randomChance < 0.4f){
//spawn Object A
}
else{
//spawn Object B
}