Question by
marisannn · Aug 15, 2016 at 03:43 PM ·
collisionontriggerenterstaticoncollisionentercounter
Add a score whenever a riceball hits the floating platform
My riceball is bouncing and the player should catch the riceball in a small platform. When the player haven't catch the riceball, it's game over. I want the score to increment by one whenever the riceball make contact with the small platform. How do I do this?
I added a script in the player sprite. The player sprite is the small platform. The score stays in zero but i want the score to increment whenever it collides with the riceball.
using UnityEngine;
public class ScorePlayer : MonoBehaviour {
private Transform tr2d;
public static int playerScore = 0;
// Use this for initialization
void Start()
{
tr2d = GetComponent<Transform>();
}
// Update is called once per frame
void OnTriggerEnter2D(Collider2D hitInfo)
{
if (hitInfo.name == "riceball")
{
playerScore += 1;
}
}
public void OnGUI()
{
GUI.Label(new Rect(Screen.width / 2 + 150, 20, 100, 100), "" + playerScore);
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612090804im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Collisions between two objects are not being detected. 1 Answer
Am I misunderstanding how this code should function? 1 Answer
Why does it display an error? 2 Answers
Need help with managing my scripts.. 1 Answer
4 line code not working! 1 Answer