- Home /
Question by
Dexer125 · Dec 05, 2017 at 03:12 PM ·
2d-platformerscorecounting
Counting and comparing score of two players
Hello I am making 2D platformer for two players and now I am stucked. I want to count score for both players and then compare it who wins. Can you help me?
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
public class SimplePlatformController : MonoBehaviour
{
public GUIText scoreText;
private int score;
public SimplePlatformController player1;
public SimplePlatformController player2;
void Start()
{
score = 0;
UpdateScore();
}
public void AddScore (int newScoreValue)
{
score += newScoreValue;
UpdateScore();
}
void UpdateScore ()
{
scoreText.text = "Korunky: " + score;
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.CompareTag("PickUp"))
{
other.gameObject.SetActive(false);
}
}
}
Comment
Answer by ShadyProductions · Dec 05, 2017 at 03:41 PM
make private int score;
a public and then you can do
var totalScore = player1.score + player2.score;
@ShadyProductions it doesn't make any sense to count the total score of both players.
It is an example on how to access the scores.
Answer by Dexer125 · Dec 12, 2017 at 07:56 PM
Hello so I have correct code but getting an error CS0119 expression denotes a value' where a method group' was expected, on if (other.CompareTag("Player1")("PickUp")) for other.
if (other.CompareTag("Player1")("PickUp"))
{
other.gameObject.SetActive(false);
count1 = count1 + 1;
SetCountText();
}