I’m trying to make a random object appear in a random location. Neither are actually truly random… I have 4 objects(weapons) and I want to spawn one at one of 3 spawn points.
I started out with this code to get my random spawn location-
//Have 3 slots in my Inspector, into which I drag/drop my 3 empty game objects (SpawnPoints)
#pragma strict
public var spawnPoints : GameObject[]; //This allows me to make a list/group of Objects
public var randomNum : int; //This denotes that the variable randomNum will be a whole number
function Start()
{
randomNum = Random.Range(0,3); // generates random number between 0 and 3.
transform.position = spawnPoints[randomNum].transform.position;
//Assigns the number generated in the previous line to the 'spawnPoints' variable
//Which then dictates which spawnPoint(location) is used (spawnPoint1, spawnPoint2, spawnPoint3)
}
This worked great and gave me 3 different spawn locations. I’ve then tried to advance the script to pick one of 4 random (suedo random) objects which will then spawn at the above location.
Gotten this far, but not sure how to tell it to pick one of the random objects?
//Have 3 slots in my Inspector, into which I drag/drop my 3 empty game objects (SpawnPoints)
#pragma strict
public var spawnPoints : GameObject[]; //This allows me to make a list/group of Objects
public var randomNum : int; //This denotes that the variable randomNum will be a whole number
public var wepType : GameObject[];
public var wepNumber : int;
function Start()
{
randomNum = Random.Range(0,3); // generates random number between 0 and 3.
transform.position = spawnPoints[randomNum].transform.position;
//Assigns the number generated in the previous line to the 'spawnPoints' variable
//Which then dictates which spawnPoint(location) is used (spawnPoint1, spawnPoint2, spawnPoint3)
wepNumber = Random.Range(0,4); // generates random number between 0 and 4.
wepType = GameObject[wepNumber];
Instantiate(wepType, spawnPoints[randomNum].transform.position);
}
I’m now getting these two error messages -
Assets/Scripts/RandomSpawnLocation.js(21,19): BCE0048: Type ‘System.Type’ does not support slicing.
Assets/Scripts/RandomSpawnLocation.js(23,20): BCE0023: No appropriate version of ‘UnityEngine.Object.Instantiate’ for the argument list ‘(UnityEngine.GameObject, UnityEngine.Vector3)’ was found.
Would really appreciate some help guys ?
(excuse all the // commenting. Its my way of trying to teach myself…lol)