check if object does NOT exist?

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

1 Like

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

1 Like

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.