- Home /
Question by
SavageX370 · Nov 21, 2018 at 06:30 AM ·
scripting problemrandom.rangeintegerpublic variable
How can I make the Random.Range in this spawning script a PUBLIC adjustable variable? The (0, 35) code limit gives errors when I need the prefab list to fluctuate. An example code would help. Thanks!
using System.Collections;
using UnityEngine;
public class PaintingSpawner : MonoBehaviour
{
public GameObject[] prefabs;
public Transform[] spawnLocations;
public float YAxisAngle;
int randPrefab;
private float nextSpawnTime;
public float spawnDelay = 10;
void Start()
{
SpawnPainting();
}
private void Update()
{
if(ShouldSpawn())
{
SpawnPainting();
}
}
public void SpawnPainting()
{
nextSpawnTime = Time.time + spawnDelay;
randPrefab = Random.Range(0, 35);
Instantiate(prefabs[randPrefab], spawnLocations[0].transform.position, Quaternion.Euler(-90, YAxisAngle, 0));
}
private bool ShouldSpawn()
{
return Time.time > nextSpawnTime;
}
}
Comment
Best Answer
Answer by dan_wipf · Nov 21, 2018 at 07:09 AM
you could do this:
randPrefab = Random.Range(0,prefabs.Length);
Thank you so much!!!!! This worked perfectly - really appreciate this answer and the SUPER helpful unity community..