- Home /
Can't figure out next step for this code ?
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)
Answer by Klarax · Jan 06, 2015 at 03:16 PM
it looks like you are using wepType in your instantiate func, which im pretty sure is an array.
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;
wepNumber = Random.Range(0,4); // generates random number between 0 and 4.
GameObject chosenWeapon = GameObject[wepNumber];
Instantiate(chosenWeapon, spawnPoints[randomNum].transform.position);
}
have a go, im tired so might be off a bit
now get this error -
Assets/Scripts/RandomSpawnLocation.js(21,19): UCE0001: ';' expected. Insert a semicolon at the end.
???
ok
do this
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()
{
print ("A");
randomNum = Random.Range(0,3); // generates random number between 0 and 3.
print ("B");
transform.position = spawnPoints[randomNum].transform.position;
print ("C");
wepNumber = Random.Range(0,4); // generates random number between 0 and 4.
print ("D");
GameObject chosenWeapon = GameObject[wepNumber];
print ("E");
Instantiate(chosenWeapon, spawnPoints[randomNum].transform.position);
}
can see where its falling over then
might be because you got non matching brackets (one open and not closing one)
Am I missing something? Surely if it won't compile, it won't play and I'm unable to see anything?
(every bracket I click has a highlighted one that corresponds)
comment out each line one by one starting backwards until it compiles. might help find the line of code breaking.
Its strange tho. only changed two lines
wepType = GameObject[wepNumber];
to
GameObject chosenWeapon = GameObject[wepNumber];
and
Instantiate(wepType, spawnPoints[randomNum].transform.position);
to
Instantiate(chosenWeapon, spawnPoints[randomNum].transform.position);
Line 25
GameObject chosenWeapon = GameObject[wepNumber];