I am wokring on a game where a player has to go through an enemy to score a point. But I am struggling to make the score count work. I am watching tutorials and trying various ways but something isnt working so I hope someone can help me here.
I have a player prefab, which gets instantiated when I press play in Unity. Then I have enemies spawning and when a player goes though one, the score should go to 1..and so on.
I have a gameObject in the scene with score script attached
public class Score : MonoBehaviour
{
public Text scoreText;
int score;
// Start is called before the first frame update
void Start()
{
score = 0;
}
public void ScoreUp()
{
score++;
scoreText.text = score.ToString();
}
}
Then I have a player script attached to a player prefab
public class PlayerController : MonoBehaviour
{
private Score score; //Reference to Score script
private void Start()
{
score = GameObject.FindWithTag("ReferenceManager").GetComponent<Score>();
}
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
//movement code
}
}
public void OnTriggerEnter2D(Collider2D collision)
{
if (collision.CompareTag("Enemy"))
{
score.ScoreUp();
Debug.Log("HIT");
}
}
}
Reference manager tag is the gameObject in the scene with score script. If someone could help me with this problem would be really awesome. Thank you
I tried the same thing but with trigger collider on its own and the score count works perfectly. So I will play around and try to figure out the correct place for that trigger collider. Thank you whoever tried to help me! :)
I had two scripts attached to scoreManager, and when i removed the other script, the score count started to work. Actually not sure why 2 scripts were conflicting but this fixed the problem.