Hey guys! I am new to unity programming. I have been trying to get a score system in place but it has not been going well. I want to make it so that every time a Game Object tagged Mean hits another Game Object named scorer (Both have a 2d box collider and a rigid body) it increments the score by 1. I have made the UI text a parent object of scorer. Here is my code. Can you guys help me make it work because for some reason my code does not seem to be working. Thanks.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class ScoreCounter : MonoBehaviour {
private int currentScore;
public Text scoreText;
// Use this for initialization
void Start () {
currentScore = 0;
}
private void HandleScore () {
scoreText.text = "Score: " + currentScore;
}
void OnCollisionEnter2D(Collision2D col) {
if (col.gameObject.tag == "Mean") {
currentScore = 1;
currentScore ++;
}
}
}
Where are you calling HandleScore? If you aren’t calling it anywhere try changing it to
Void Update() instead & leave the code you have for displaying the text.
Also, it looks like your score will always display as 2 as every time you collide with Mean you set the score to one then increment it by one.
This still is not working. Could it be the way I have set everything up. I created an empty Game Object witha 2d rigid body and box colider. I made this the parent object of both the canvas and text UI. I added the script to the text. Could this be where I am going wrong? Thanks.
You need a ground that you move on, an object that has been tagged Mean, & a player.
Uncouple the UI so it isn’t on any object.
Put the script on your player (the collision detection needs to be on one of the items that are colliding, in this case the player since it is checking to see if it has collided with something tagged Mean.
Have you done any of the tutorials? Start with them before you start trying to do stuff on your own as they will take you through everything step by step & explain why they did things the way they did.
It’s good that you’re trying to help but please look at the dates of posts before replying. You’re necroing a post from 7 years ago and this isn’t likely a current problem.