Give point on button click
Hello,
I want people to get 1 point when they click on a button.
So on top of the android app I have: Current ammount
And below that: Click to get a point
I have no idea how to start, anyone knows a tutorial?
Thanks
Answer by TBruce · Oct 02, 2016 at 08:14 PM
What is the player clicking? A button? An image/sprite? Do you have any code?
If the player is clicking a button you can do some thing like this
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class ButtonClickScript : MonoBehaviour
{
public Button button;
public Text text;
public int currentPoints = 0;
public void ClickButton()
{
if (button != null)
{
currentPoints++;
if (text != null)
{
text.text = "Current Points" + currentPoints.ToString();
}
}
}
}
The ClickButton
is set up as an event. Here is a little demo of how to do this.
To learn more about this see the Unity Button docs and these tutorials
Now if the player is clicking an image/sprite then you first need to make sure that there is a collider on the game object that has the image/sprite (most likely a Boxcollider2D). You can then do something lie this
using UnityEngine;
using System.Collections;
public class ImageClickScript : MonoBehaviour
{
public Text text;
public int currentPoints = 0;
void OnMouseDown ()
{
currentPoints++;
if (text != null)
{
text.text = "Current Points" + currentPoints.ToString();
}
}
}
@$$anonymous$$windo Could you please be so kind as to click the "Accept" button above to accept the answer if you have found this helpful?
@$$anonymous$$avina I already found the answer but still thank you!
I am happy that everything is working for you then.