I’ve watched several tutorials, read trough several other similar questions, and my score still doesn’t increase, neither did i manage to show it on screen. I’ve spent hours trying to implement this very simple feature and i’m just stuck on it.
The only part i got woking is destroying coins after they’re collected, but nothing more.
Currently, i’m using these scripts for player and score:
Player:
public class Player : MonoBehaviour
{
public float speed;
public Rigidbody2D rb;
Vector2 movement;
public int coins;
public AudioSource collectSound;
public Text score;
void FixedUpdate()
{
movement.x = Input.GetAxisRaw("Horizontal");
movement.y = Input.GetAxisRaw("Vertical");
rb.MovePosition(rb.position + movement.normalized * speed * Time.fixedDeltaTime);
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Coin"))
{
collectSound.Play();
Score.AddScore(1);
Score.AddCoins(1);
Destroy(collision.collider.gameObject);
}
}
}
Score:
public class Score : MonoBehaviour
{
public GameObject scoreText;
public static int score = 0;
public static int coins = 0;
void FixedUpdate()
{
scoreText.GetComponent<Text>().text = "Score: " + score;
}
public static void AddScore(int addscore)
{
score += addscore;
}
public static void AddCoins(int addcoins)
{
coins += addcoins;
}
}
In the meantime, I give up on trying to add the score system, and i’ll implement other features.