- Home /
Player spawn after touching
Help, im trying to make my player spawn when the game starts and when he dies from touching a killzone tag. Yes this may not be the most efficient way to do by if you want you can fix it up, im getting 3 errors on line 26: 1. 'expecting ), found : ' 2. ';' expected. Insert a semicolon at the end 3. Unexpected token: ).
Plz help i need this ASAP, and i have no idea what is wrong, i don't even know how to use arrays but anything would be helpful, it is a merge of two scripts one detecting weather you have hit a killzone and the spawn script. i have made shore that there are both java script Sorry for my bad spelling
private var timer : float = 0.0;
private var spawning : boolean = false;
var prefab : GameObject;
var numspawns : float = 0;
var spawn1 : Transform;
var spawn2 : Transform;
var spawn3 : Transform;
var spawn4 : Transform;
var spawn5 : Transform;
var spawn6 : Transform;
var spawn7 : Transform;
var spawn8 : Transform;
var spawn9 : Transform;
var spawn10 : Transform;
var spawn11 : Transform;
var spawn12 : Transform;
function OnControllerColliderHit(hit : ControllerColliderHit)
{
if(hit.gameObject.tag == "Killzone")
{
Spawn();
}
else
{
OnControllerColliderHit(hit : ControllerColliderHit);
}
}
function Spawn()
{
//set spawning to true, to stop timer counting in the Update function
spawning = true;
//reset the timer to 0 so process can start over
timer = 0;
//select a random number, inside a maths function absolute command to ensure it is a whole number
var randomPick : int = Mathf.Abs(Random.Range(1,numspawns));
//create a location 'Transform' type variable to store one of 3 possible locations declared at top of script
var location : Transform;
//check what randomPick is, and select one of the 12 locations, based on that number
if(randomPick == 1){
location = spawn1;
Debug.Log("Chose pos 1");
}
else if(randomPick == 2){
location = spawn2;
Debug.Log("Chose pos 2");
}
else if(randomPick == 3){
location = spawn3;
Debug.Log("Chose pos 3");
}
else if(randomPick == 4){
location = spawn4;
Debug.Log("Chose pos 4");
}
else if(randomPick == 5){
location = spawn5;
Debug.Log("Chose pos 5");
}
else if(randomPick == 6){
location = spawn6;
Debug.Log("Chose pos 6");
}
else if(randomPick == 7){
location = spawn7;
Debug.Log("Chose pos 7");
}
else if(randomPick == 8){
location = spawn8;
Debug.Log("Chose pos 8");
}
else if(randomPick == 9){
location = spawn9;
Debug.Log("Chose pos 9");
}
else if(randomPick == 10){
location = spawn10;
Debug.Log("Chose pos 10");
}
else if(randomPick == 11){
location = spawn11;
Debug.Log("Chose pos 11");
}
else if(randomPick == 12){
location = spawn12;
Debug.Log("Chose pos 12");
}
//create the object at point of the location variable
var thingToMake : GameObject = Instantiate(prefab, location.position, location.rotation);
thingToMake.AddForce(Vector3(0,0,100));
//halt script for 1 second before returning to the start of the process
yield WaitForSeconds(1);
//set spawning back to false so timer may start again
spawning = false;
}
Edit your post and mark the code with the 101010 icon on the toolbar.
you can now edit the post, i cant get the code thing to work
First: your OnControllerColliderHit calls itselfs if hit.gameObject.tag is not equal to "$$anonymous$$illzone", making an Infinite loop.
Second: $$anonymous$$athf.Abs() won't return an integer, it will only return a positive float, Random.Range() will return an integer if both arguments is integers, so changing your numspawns-variable to an integer and removing the $$anonymous$$ath.Abs will fix it (your variable 'location' has no values as the randomPick isn't integer)
It would be alot easier to know what your compiling errors are for if you would comment on what line is nr 26. Also it would be alot easier to do the locations with an array, you could just write "location = spawn[randomPick];" ins$$anonymous$$d of all the if checks :)
Answer by Kleptomaniac · Mar 08, 2012 at 02:18 PM
I think this code is in desperate need of a bit of tidying up and simplifying. Give @FishBone props for this one because he hit the nail right on the head in all respects.
private var timer : float = 0.0;
private var spawning : boolean = false;
var prefab : GameObject;
var spawns : Transform[];
function OnControllerColliderHit(hit : ControllerColliderHit)
{
if(hit.gameObject.tag == "Killzone")
{
Spawn();
}
}
function Spawn()
{
//Timer and spawning vars are not being used - Residual vars from previous script? - Left them in anyway
//set spawning to true, to stop timer counting in the Update function
spawning = true;
//reset the timer to 0 so process can start over
timer = 0;
//Select a random number (from a range of ints)
var randomPick : int = Random.Range(0, spawns.Length);
//Instead of a Transform, you want a Vector3 here
var location : Vector3;
//Check what randomPick is, and select one of the 12 locations, based on that number
location = spawns[randomPick].transform.position;
Debug.Log("Chose pos " + randomPick);
//create the object at point of the location variable
var thingToMake : GameObject = Instantiate(prefab, location, Quaternion.identity);
thingToMake.AddForce(Vector3(0,0,100));
//halt script for 1 second before returning to the start of the process
yield WaitForSeconds(1);
//Again, left spawning in
//set spawning back to false so timer may start again
spawning = false;
}
There you go, that should work! Again, remember to give FishBone an upvote for this one!
Klep
Changing to an array you should also change the randompick to include 0. Also would be better to use spawns.Length ins$$anonymous$$d of numspawns to not get an error if you don't make 12 spawn-points.
So it should be:
var randomPick : int = Random.Range(0,spawns.Length);