Hello
i wrote a little clicker game where you click the screen and get a point now I am trying to add a auto clicker that adds every second one point, but now I cant click manually on the screen to get a point.
Thanks for every answer!
Thats my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ClickCount : MonoBehaviour
{
public Text displayPoints;
public SafeAndLoad safeAndLoad;
private float Points;
void Update()
{
Count();
displayPoints.text = "Points: " + safeAndLoad.points;
}
void OnMouseUp()
{
safeAndLoad.points += safeAndLoad.pointsPerClick;
}
void Count()
{
Points += Time.deltaTime * safeAndLoad.pointsPerTick;
safeAndLoad.points = Mathf.RoundToInt(Points);
}
}
Solved it by my own if somebody has the same problem heres the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ClickCount : MonoBehaviour
{
public Text displayPoints;
public SafeAndLoad safeAndLoad;
private float Points;
void Update()
{
Points += Time.deltaTime * safeAndLoad.pointsPerTick;
if(Points >= 1.0f)
{
safeAndLoad.points += 1;
Points = 0.0f;
}
displayPoints.text = "Points: " + safeAndLoad.points;
}
void OnMouseUp()
{
safeAndLoad.points += safeAndLoad.pointsPerClick;
}
}
I noticerd a small bug in your new code:
if(Points >= 1.0f)
{
safeAndLoad.points += 1;
Points = 0.0f;
}
If points was, say 1.05, you’re throwing out that extra .05. That means your points will accrue at a slightly slower rate than you expect. I would replace that code with this:
while (Points >= 1.0f)
{
safeAndLoad.points += 1;
Points -= 1f;
}
With this code, if Points is 1.05, you will add 1 to your score and keep the leftover .05. This will also handle the case where you end up with e.g. 2.05 points, since it’s a while loop.
1 Like