- Home /
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 :D
I'm super new to this, thanks :)
/* 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[i].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?
Answer by save · Mar 09, 2012 at 12:19 PM
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 thought static variables could be referenced everywhere :(
So I replace the bottom code of $$anonymous$$e, with yours? and I tag my Players with 'player' tag? $$anonymous$$y top script is called 'dead count' so i reference that in " other.GetComponent(PlayerScript) " - so it needs to be attached to the player?
Sorry, I'm learning. :)
No need to say sorry, it's good you ask questions. :)
You can reference to them from anywhere just as I described, you can only declare them once though. By calling: AnotherScript.aStaticVariable, you reference to a static variable in the other script.
You use GetComponent if the script isn't on the object, for instance if the trigger is a lava lake and the player should emidiately die when entering the trigger. If the script is on the player, variable "dead" needs to be private or regular (non-static) - if you have several players (which you seem to have).
Answer by LittleCat1 · Mar 09, 2012 at 06:02 PM
I think I'm confusing myself now :(
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[i].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.
Try to use the comments ins$$anonymous$$d of posting an answer to your own question (Quato is a bit different from regular forums). Ok so let's see.. First off name the script DeadCount without spaces.
var DEAD : int = 0; need to be outside Update(), otherwise you'll continuously set that variable to 0 every frame and it'll only be accessible inside Update.
for-loop looks a bit weird. Is it a for-loop you want there?
//Example of for-loop:
for (var i : int = 0; i < 4; i++) {
//Code goes here
}
You're right, ++ adds to an int each time processed:
myIntVariable++;
I think you'd be better off with a script that acts as a manager for all your players. Then you could pass a call for a function like so:
function OnTriggerEnter (other : Collider) {
if (!other.CompareTag("Player")) return;
var thePlayerScript : ScriptOnPlayer = other.GetComponent(ScriptOnPlayer); //Get the script on player - Name it accordingly!
thePlayerScript.kill() //Call a function on the player
}
//A part in the thePlayerScript:
private var dead : boolean = false;
private var myNumber : int = 0;
function $$anonymous$$ill () {
dead = true;
Game$$anonymous$$anager.$$anonymous$$illPlayer(myNumber);
}
//On a script called Game$$anonymous$$anager
static function $$anonymous$$illPlayer (who : int) {
//who is now the same int as the player that called it, you can use it as reference for anything
}
//The same thing when a player gets added to the game, by calling:
static var playersAmount : int = 0;
static function JoinPlayer () {
playersAmount++;
return playersAmount;
}
So in the player's script you could add:
function Start () {
myNumber = Game$$anonymous$$anager.JoinPlayer();
}
Hope it makes sense, it seems like you're struggling with typing the code right, perhaps you should go through a couple of more tutorials and read through the scripting reference before you throw yourself at this stuff. It takes a bit of time to grasp the concept, so give it time. ;)
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Problems Scripting a Camera 1 Answer
fix sonic like script? 1 Answer
[SOLVED] Enemy Script : Mob doesn't take damage 2 Answers
Destroy gameObject not working? 3 Answers