Hi guys, i have a target and m using Input.GetButtonDown() function to hit on target,, now i have a condition set which will print message if mouse crsor is placed on target and if i click on target it will print message "hit on target",, if clicked out of target it will print message "hit miss",, now i want to count no of hit and no of miss based on hit position,, please help
pseudo code:
int clickCount = 0;
int hitCount = 0;
bool canFire = true;
...
int GetMisses()
{
return (clickCount - hitCount);
}
int GetHits()
{
return hitCount;
}
...
void Update()
{
if (Input.GetButtonDown("A") && canFire == true)
{
canFire = false;
clickCount++;
if (hitTarget)
{
hitCount++;
}
}
else if (Input.GetButtonUp("A"))
{
canFire = true;
}
}
I strongly advise you to read a book about programming in general. The easiest way to achieve what you want is to have two variables in the class that you count up each time.
This is a partial implementation of what it might look like in c#
public class Something : MonoBehaviour { private int misses; private int hits; void Start() { misses = 0; hits = 0; //.. and whatever else might need to be initialized } //.. some other code and finally your function that determines if there was a hit: private void processHit() { //.. wasHit stands for whatever code you have that says that it was a hit if (wasHit) { ++hits; } else { ++misses; } //.. } }
The variables will contain the most recent count of hits and misses.. That's really much much more basic than actually determining if there was a hit or not, but I guess you got the script from somewhere. If you really want to dig into game programming you need to start to understand programming itself.. just my word of advice.
Now if you have that script running on many objects and need a global counter in your scene, there are many ways to achieve it. I show you a simple version, but it is not the best solution as this is not threadsafe - but maybe you don't have to worry about that just now.
public class HitCounter { private static int hits = 0; private static int misses = 0; public static int Hits { set {hits = value;} get {return hits;} } public static int Misses { set {misses = value;} get {return misses;} } }
Now you can use this HitCounter everywhere like so:
if (UnityEngine.Random.value > 0.5f) { ++(HitCounter.Hits); } else { ++(HitCounter.Misses); } Debug.Log("hits: " + HitCounter.Hits + " misses: " + HitCounter.Misses);