Adding points for every gameObject on the scene instead of adding points for the destroyed one
Hello everyone! I'm new at programming and I'm doing a prototype for a simple game.
I added Touch functions to the script, so the player can destroy the enemies in the screen by touching on it. The problem that I have is when I touch the enemy, it is destroyed but the score amount that is added is the total of all enemies on scene, instead of the score of the one that was destroyed. This only happens with the Touch controls, with the mouse click it's all right.
void TouchController()
{
GameObject selectedGameObject;
if (Input.touchCount > 0)
{
if (Input.GetTouch(0).phase == TouchPhase.Began)
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
if (Physics.Raycast(ray, out hit))
{
if (hit.collider.gameObject.tag == "Enemy")
{
selectedGameObject = hit.collider.gameObject;
Destroy(selectedGameObject);
ScoreManager.AddPoints(pointsToAdd);
}
}
}
}
}
Thanks in advance!
Comment
Your answer
Follow this Question
Related Questions
Scripting Errors 1 Answer
Problem in raycast to detect touch in 2D game 1 Answer
How to detect a touch inside a GUI 0 Answers
GraphicRaycaster with Camera Space UI 1 Answer
C# Touch Input with Raycast on Interactive Map to toggle Canvas 0 Answers