- Home /
if others are dead, do this
Hi, again :) I'm trying make it so that when a character falls off the edge and collides with my fallour floor - He's classed as dead, and lets say in a four player game, when the 3 other opponents are dead, the 1 alive player (dead=false) wins.
What I'm asking for is there a way to check if 3 of my 4 charaters are dead, and if so, to open up a winscreen or gui depending on winner "Player 1 wins".
private var dead = false;
function OnTriggerEnter (other : Collider)
{
dead = true;
}
Thank you so, so much :)
Using a for loop will do the trick in your case. $$anonymous$$ore information on for loops (as they can be really specific) can be found here: http://answers.unity3d.com/questions/11547/how-do-i-use-for-to-create-loops-in-my-script-and.html
Answer by FishBone · Mar 08, 2012 at 03:06 PM
If you have a list of your players gameobjects somewhere, make the dead-variable public and loop through the players to check how many of them is dead.
var deadPlayers : int = 0;
for(int i=0; i<_players.Length; i++){
if (_players[i].dead)
deadPlayers++;
}
if (deadPlayers >= 3)
ShowWinScreen();
could always use a get and set property that increments each death and would check to see if it was greater than the number of players. Don't know the syntax for unityscript, but it seems like that would be an efficient way of doing so ins$$anonymous$$d of looping every frame.
Thanks :) Sorry, It's been a bit of a long day, and I'm a bit shattered. What do I actually need to do? Turn deadPlayers into a static var
static var deadPlayers = false;
function OnTriggerEnter (other : Collider) {
deadPlayers = true;
Debug.Log (other.gameObject.name);
}
&
static var deadPlayers : int = 0;
function Update (){
for(int i=0; i<_players.Length; i++){ if (_players[i].dead) deadPlayers++; } }
if (deadPlayers >= 1) ShowWinScreen();
nomatter what I do, I'm still getting errors as it's not reading it as a integer (ecpecting; found "i" - 3rd line, 7th char)
sorry not used to java, it should be
for (var i:int = 0; i<_players.Length; i++) {
But what SirGave said is true, so you should maybe do it that way ins$$anonymous$$d.
Thanks :) Still getting errors, but I think I'll just have to fiddle more. How add my 'players' to the _Players identifier?
@Sir Give/Anyone - I have no idea how to impliment this, is there anychance you could write something up for me? I know it's a huge ask, but I'd be forever grateful :) It really doesn't matter if it's in C# as I've got a friend that'll 'translate' over to Java if needed.
Thanks for being awesome :)
Your answer
Follow this Question
Related Questions
Can't click gameobject when over another trigger? 1 Answer
flickering lights when player gets near 1 Answer
Projectile reflection 1 Answer
Putting an object somewhere to destroy something else 1 Answer
Bullet triggers wrong collider 1 Answer