How do you check if an object does not exist (it existed previously but was destroyed)? Can I see some pseudocode?
Actual code:
if (!someObject) {
// someObject doesn't exist
}
âEric
Sorry to resurrect an ancient thread, but I am trying to do something like this again now using the following code:
var target: Transform;
var respawntarget: Transform;
function Update () {
if (!target) {
Instantiate(respawntarget, Vector3(200, 5, 108), Quaternion.identity);
}
}
âtargetâ is the player as he is at the game start. ârespawntargetâ is a prefab of the player. When the player dies, the game wants to respawn an infinite number of players. How do I stop this?
use target != null not !target as the boolean check is not implemented everywhere.
as for the infinite number: donât respawn in update, instead respawn in the âdieâ routine that facilitates the dieing by firing off a coroutine for the respawn
Something like this?
var target: Transform;
var respawntarget: Transform;
function Respawn () {
if (target != null) {
Instantiate(respawntarget, Vector3(200, 5, 108), Quaternion.identity);
}
function Update () {
StartCoroutine(Respawn);
}
}
This gives an error: Assets/PlayerRespawnScript.js(7,10): BCE0044: expecting (, found âUpdateâ.
No, you donât want to use Update at all; itâs spawning an infinite number of Respawn coroutines. As for that particular error, make sure you indent your code properly so you can see where you need to match braces.
âEric
Thanks. So where do I call the coroutine from if not from an update?
As dreamora said, call it when the object is destroyed. It doesnât have to be a coroutine though, just a normal function is fine.
âEric
var target: Transform;
var respawntarget: Transform;
function CheckForPlayer () {
if (target != null) {
StartCoroutine(Spawn);
}
}
function Spawn () {
Instantiate(respawntarget, Vector3(200, 5, 108), Quaternion.identity);
}
This should work, right? But it gives me an error saying that I need a semicolon at the end of line 5, even though one is already there.
The correct syntax is âStartCoroutine(Spawn());â, however in JS you can just do âSpawn();â. Using StartCoroutine wonât work unless the function actually is a coroutine, but as I mentioned thereâs no reason for it to be one, so I would recommend not using it. Although again I would also recommend not doing this check in the first place, but just call the Spawn function when the object is destroyed.
âEric
var target: Transform;
var respawntarget: Transform;
function CheckForPlayer () {
if (target != null) {
Spawn();
}
}
function Spawn () {
Instantiate(respawntarget, Vector3(200, 5, 108), Quaternion.identity);
}
Still not working.
Instantiate will make a new instance. So target will still be null. You probably want something like target = Instantiate(âŚ), otherwise itâll keep spawning new instances because target never gets assigned.
@niosop:
Exactly how would I fit that into the script?
Okay, Iâve just written the following script which combines the health and respawn scripts into one:
static var lives = 3;
var PlayerHitpoints = 4;
var projectile: Transform;
function OnCollisionEnter () {
if(projectile.CompareTag ("EnemyProjectile"))
--PlayerHitpoints;
if (PlayerHitpoints == 0)
--lives;
}
function Update () {
if (lives == 2)
Application.LoadLevel (1);
if (lives == 1)
Application.LoadLevel (1);
if (lives == 0)
Application.LoadLevel (2);
}
This code reloads the level after the player dies, like I want, but when this is finished none of the controls for movement work.