Dead Collision

Hi, I’ve been given this nice bit of code by someone nice on here - and I’m really struggling with it. It’s meant to count my players that get the ‘dead’ variable (which should turn on when they fall off edge and collide with the floor - I’ve got a feeling it might be global, and not only on the object though, which may need to fix). I also don’t really understand the top part, could someone please shed some light on it, please? If someone does rewrite it - UnityScript or C#, it doesn’t matter, I’ve a terrible knowledge of both :smiley:

I’m super new to this, thanks :slight_smile:

 /* Hooks in said variable count */
static var dead : int = 0;

/* Update on each frame */
function Update() {

var dead /*Players*/ : int = 0;

/* Declaring that var i is int (which is 0) */
for (var i: int = 0)

// I don't understand at all
// ++ is add one each time, isn't it?

  i<_players.Length; i++) {
    if (_players*.dead)*

> deadPlayers++;
> }
>
> /* Counts dead Players - if >=3, do next */
> if (deadPlayers >= 3)
>
> /*Load Level */
> Application.LoadLevel(1);
> }
&
> // Static Var makes public
> static var dead = false;
>
> // does the dead tag apply globally? or just to the object that
> collided?
> // Because I want it just on the object
>
> function OnTriggerEnter (other : Collider)
> {
> dead = true;
> Debug.Log (other.gameObject.name);
> }
TLDR:
Can someone explain to me the top part of code/fix it for me?
Does the bottom bit of code make the dead variable global, or just on the object collided - How can I change that?
Do I need to declare my characters, so they can be counted?

OnTriggerEnter is only happening on the object it's attached to and only if it has a collider set to trigger.

Static variables can only exist singularly within the namespace. If you want to refer to a static variable you type `TheScriptName.dead = true;` But I assume that every player can die so it should be a non static variable thus making it available to be different on different objects.

If the trigger is not on the player then you can do like this:

function OnTriggerEnter (other : Collider) {
    if (!other.CompareTag("Player")) return;
    var pScript : PlayerScript = other.GetComponent(PlayerScript); //Name of the player's script
    pScript.dead = true;
}

I think I’m confusing myself now :frowning:

The on trigger enter is on the ‘lava’ and searches for tags named player and makes to dead.true if player by using Dead Count.true (name of script is called Dead Count). What’s the pscript var about?

Also, I know it’s asking an awful lot, but is there any chance you could look at this and see what’s happening?

The code you gave me which works (commented out pscript, as I’m not sure it’s purpose and it’s attached to the ‘lava’, not a player)

function OnTriggerEnter (other : Collider) {
    if (!other.CompareTag("Player")) return;
    //var pScript : PlayerScript = other.GetComponent(Dead Count); 
    //Name of the player's script
    DeadCount.DEAD = true;
    Debug.Log ("Dead Count.dead = true;");
}

and this second script that the first refers to.
Everything is commented out so it doesn’t give me nasty errors

//Hooks to other file (DeadCollision) to gain whether dead

or not - i think.
static var DEAD = false;

//Now I need a way to count how many have 'DEAD' tag, and if more than

3 or more, end game.
//I have 4 players
//need to count the ones with DEAD tag - not sure on code on how to do
so.

//function Update() {
//var DEAD: int = 0;

/* Declaring that var i is int (which is 0) */
//for (var i: int = 0)

// I don't understand at all
// ++ is add one each time, isn't it?

  //i<_players.Length; i++) {
   // if (_players*.dead)*

> // deadPlayers++;
> //}
>
> /* Counts dead Players - if >=3, do next */
> //if (deadPlayers >= 3)
>
> /*Load Level */
> // Application.LoadLevel(1);
> //}
Sorry, been doing this like a week now, and issues like these are really stumping me.