• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by san042 · May 03, 2011 at 05:59 AM · javascript

Counting number of hit and no of miss

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by loopyllama · May 03, 2011 at 06:27 AM

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; } }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0
Best Answer

Answer by Tom 17 · May 03, 2011 at 06:40 AM

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);
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Setting Scroll View Width GUILayout 1 Answer

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Getting data for if an object has already been collected on startup 1 Answer

Unity says that my script isn't doing anything? 1 Answer

How to Rotate on one axis? 4 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges