- Home /
Problems with a player spawning and handling script
Here's the script,
var spawnpoints : GameObject [];
var isalive : boolean = true;
var playerprefab : GameObject;
var player : GameObject;
var Target : GameObject;
InvokeRepeating("checkalive", 0, 0.0001);
function checkifalive () {
if(player.HP < 1){
isalive == false;
respawn();
}
}
function FindTarget () : GameObject {
if(GameObject.FindWithTag("Finish")){
who = GameObject.FindWithTag("Finish"); //We have found a Target
}
return who;
}
function Update () {
if(!Target){
Target = FindTarget(); return;
}
}
function respawn (){
yield WaitForSeconds(3);
var index : int = Random.Range(1,spawnpoints.length);
var thespawn : GameObject = spawnpoints[index];
var instance : GameObject = Instantiate(playerprefab, thespawn, transform.rotation);
player = instance;
isalive == true;
}
there are two errors im encountering with this, and every time i think i fixed it, it gets worse, so I've stopped and resorted to unity answers and the guides.
How the script is supposed to work, is that the checkifalive function checks every one 10 thousandth of a second if the player gameobjects HP value is more than one. if it isnt, it checks the isalive boolean to false and respawns the player by using the playerprefab variable.
the respawn function supposedly should spawn the playerprefab randomly in one of the spawnpoints, and set the isalive value back to true.
there are two errors, the first is
Assets/scripts/Deathmatch/deathmatchHealth.js(19,17): BCE0034: Expressions in statements must only be executed for their side-effects.
and
Assets/scripts/Deathmatch/deathmatchHealth.js(52,44): BCE0023: No appropriate version of 'UnityEngine.Object.Instantiate' for the argument list '(UnityEngine.GameObject, UnityEngine.GameObject, UnityEngine.Quaternion)' was found.
again, I'm not sure how to fix these without making it worse, any help is appreciated.
Answer by BlackHoleStorm · Apr 26, 2014 at 09:30 PM
You have your Instantiate outside of a function, so it isn't running.
So instead of this
InvokeRepeating("checkalive", 0, 0.0001);
use
function Start()
{
InvokeRepeating("checkalive", 0, 0.0001);
}
or just add checkalive(); to your Update function
The first error because you have two equal signs (an equality test like an if) but you should only have one. The Invoke was an issue but that's fixed per your last comment, however an invoke that often to be honest is weird and should be rethought. Line 30 looks odd to me; Add
#pragma strict
to the very top of this and see if you get further or more info on errors.
Finally, thespawn is a GameObject but the 2nd argument should be a position like thespawn.transform.position
I've just read what getyour411 said and the only other problems I can see is you set "isAlive" with 2 equal signs as he said. I'm not really a JS guy so sorry I can't help much
Your answer
Follow this Question
Related Questions
Respawn Player after Health 0 1 Answer
Restart with GUIText 2 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
c# how to change player location on 0 hp 1 Answer
Taking a hit 3 Answers