Increment score by only 1?

So i want to increment my score by 1 but instead it goes wild and adds like 50/second endlessly.

Each of my players have a timer variable so Unity can tell who won by who had the fewest seconds.

``````    if(TotalTimeA < TotalTimeB)
{
Player1Won = true;
}
if(TotalTimeA > TotalTimeB)
{
Player2Won = true;
}
}
``````

When etc. Player1Won = true, it will tell the scoreboard script that player1´s score should increment by 1.
But then the bug shows its fail and keep incrementing endlessly.

``````function FixedUpdate ()
{
// Player 1

if(timer.Player1Won)
{
Player1Score++;

}

// Player 2

if(timer.Player2Won && Player2ScoreOld ==0)
{
Player2Score++;
}

}
``````

Help is very appreciated!

Had this issue. You can make a workaround like so:

``````var player1won : bool = false;

function Update() {
if (player1.won && !player1won) {
player1score++
player1won = true;
}
}
``````

In other words you have a boolean which limits your action to only 1 time.

Could also be done more simply by doing:

``````function Update() {
if (player1.won) {
player1score++
player1.won = false;
}
}
``````

There’s no bug, your logic is just faulty. Try increasing the score when you decide who has won, rather than in a separate section that’s called constantly.

2 Likes

I tried the following but it still increase increment it endlessly

``````    if(timer.Player1Won)
{
Player1Score++;
timer.Player1Won = false;

}
``````

So, you would like to increase the score by 1 every second?
Or you would like to restict the score to a maximum of 1?

Could you provide complete code for both your scripts (with TotalTimeA, TotalTimeB and Player1Score, Player2Score)?
I think you can make it easier.

I would like to increase the score only by 1 everytime a player wins.
Example: Player 1 wins round 1 so he gets 1 point.
Example: Player 1 wins round 2 and the score is now 2 - 0

Timer Script

``````#pragma strict

//Reference
private var player1 : GameObject;
private var playerStat : PlayerStat;
private var player2 : GameObject;
private var playerStat1 : PlayerStat1;

private var gm : GameObject;
private var scoreboard : Scoreboard;
var ScoreboardObject : GameObject;

//Total Time
static var TotalTimeA = 0;
static var TotalTimeB = 0;

static var ShowtotalA = false;
static var ShowtotalB = false;

//player 1
static var Timer1A : float;
static var Timer2A : float;
static var Timer3A : float;

public static var Show1A = false;
public static var Show2A = false;
public static var Show3A = false;

static var IncreaseT1A = true;
static var IncreaseT2A = false;
static var IncreaseT3A = false;

public static var Restart1;

//player 2
static var Timer1B : float;
static var Timer2B : float;
static var Timer3B : float;

public static var Show1B = false;
public static var Show2B = false;
public static var Show3B = false;

static var IncreaseT1B = true;
static var IncreaseT2B = false;
static var IncreaseT3B = false;

public static var Restart2;

// GENERAL

static var FinishedA = false;
static var FinishedB = false;

static var Player1Won = false;
static var Player2Won = false;

static var BothDone = false;

//////////////////////////////////////////////

function Awake ()
{
player1 = GameObject.FindGameObjectWithTag("Player1");
playerStat = player1.GetComponent(PlayerStat);
player2 = GameObject.FindGameObjectWithTag("Player2");
playerStat1 = player2.GetComponent(PlayerStat1);

gm = GameObject.FindGameObjectWithTag("Gamemaster");
scoreboard = gm.GetComponent(Scoreboard);
}

function Update ()
{
//Player 1

//Start
if(IncreaseT1A == true)
{
Timer1A += Time.deltaTime;
}
if(IncreaseT2A == true)
{
Timer2A += Time.deltaTime;
}
if(IncreaseT3A == true)
{
Timer3A += Time.deltaTime;
}

//Player 2

//Start
if(IncreaseT1B == true)
{
Timer1B += Time.deltaTime;
}
if(IncreaseT2B == true)
{
Timer2B += Time.deltaTime;
}
if(IncreaseT3B == true)
{
Timer3B += Time.deltaTime;
ShowtotalB = true;
}

if(Show3A == true && Show3B == true)
{
BothDone = true;
}
if(BothDone)
{
ScoreboardObject.SetActive(true);
}
}

function FixedUpdate ()
{
//General
if(BothDone)
{
TotalTimeA = Timer1A + Timer2A + Timer3A;
TotalTimeB = Timer1B + Timer2B + Timer3B;
}
// Scoreboard

if(TotalTimeA < TotalTimeB)
{
Player1Won = true;
}
if(TotalTimeA > TotalTimeB)
{
Player2Won = true;
}
}
``````

Score Script
```javascript
**#pragma strict

private var gm : GameObject;
private var timer : Timer;

var Player1Score = 0;
var Player2Score = 0;

var Player1ScoreOld : int = 0;
var Player2ScoreOld : int = 0;

function Start ()
{
gm = GameObject.FindGameObjectWithTag(“Gamemaster”);
timer = gm.GetComponent(Timer);
}
function Awake ()
{
}

function FixedUpdate ()
{
// Player 1

``````if(timer.Player1Won)
{
Player1Score++;
timer.Player1Won = false;
}

// Player 2

if(timer.Player2Won && Player2ScoreOld ==0)
{
Player2Score++;
timer.Player2Won = false;
``````

}**
```

FixedUpdate is used for physics based actions… you have no physics based code in there… really should be using Update() instead of FixedUpdate().

as for the rest… you seem to be obsessed with variables and just the update functions which looks to be messing up the logic… can you explain what it is you are trying to create (i.e. the design of the game).

Ball rolling game.

The 2 players will compete in the same map each having 3 rounds to try and get the lowest total time.
It will then put the 2 players total time together and find the winner by picking the 1 with the lowest total time.

I then want it to give the player with the lowest timer 1 point.

And then it changes map and they will again compete to have the lowest total time and be awarded 1 point.