I recently started using unity and I am trying to make a 2D game where symbols (square, cross, triangle or circle) are spawned and fly left towards the player and the player have to shoot projectiles of the same type.
I got the basics working but now I am trying to add score which would increase by 1 every time player succesfully matches the enemy symbol (when the two objects collide). The score increases but only once and then stays the same for every collision after and I just cant figure out why it is no working.
Every response will be apreciated.
(Sorry for my english, migh have some mistakes)
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class Enemy : MonoBehaviour
{
public float speed;
public Text scoreText;
private int myScore = 0;
private void Start()
{
scoreText = GameObject.FindGameObjectWithTag("Score").GetComponent<Text>();
}
void Update()
{
transform.Translate(Vector2.left * speed * Time.deltaTime);
}
private void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("Cannon"))
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
if (other.tag == gameObject.tag)
{
myScore += 1;
Debug.Log(myScore);
scoreText.text = myScore.ToString();
Destroy(other.gameObject);
Destroy(gameObject);
}
else
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
}
}

