Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 /
  • Help Room /
avatar image
0
Question by Sletting96 · Jan 27, 2016 at 08:12 PM · c#scorescore systemactivatescores

How do I get points when clicking an object?

Hi.

I'm new to Unity scripting. I've made two scripts. One that adds 1 point to my score when I click on the object with the script and one that manages it and displays it on my HUD. It kinda works, but I can do it from all lengths. I want to be in reach of the object before I get points. How can I do that?

 public class Counter : MonoBehaviour {
 
     private float pettingFinished = 0;
 
     public void OnMouseUp()
     { 
         Debug.Log("I've been clicked"); //For debugging purpose
 
         if (Time.time > pettingFinished)
         {
             ScoreCounter.score += 1; //Adds 1 point to the score
 
             pettingFinished = Time.time + 1;
         }     
     }
 }

the other script:

 public class ScoreCounter : MonoBehaviour {
     static public int score = 0; //player score
     Text uiText;
 
     void Awake()
     {
         uiText = GetComponent<Text>();  //Set up the reference.
         score = 0;  //Reset the score.
     }
 
     void Update ()
     {
         uiText.text = "Score: " + score;    // Set the displayed text to be the word "Score: " followed by the score value.
     }
 }


Thanks in advance

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Jessespike · Jan 27, 2016 at 08:38 PM

Have you tried Vector3.Distance? You'll need to provide positions for the distance calculation.

 float distance = Vector3.Distance( playerTransform.position, pointTransform.position);
 if (Time.time > pettingFinished && distance < 3f)
 {
      ScoreCounter.score += 1; //Adds 1 point to the score

Comment
Add comment · Show 2 · 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 Sletting96 · Jan 27, 2016 at 08:42 PM 0
Share

I haven't tried that. Sadly, I do not understand how it works.

I know that Vector3 is something to do with the position (x,y,z), but I don't know much more than that.

Can you elaborate?

avatar image Jessespike · Jan 27, 2016 at 09:14 PM 0
Share

Not sure if I can explain clearly. Here goes:

Vector3 is a struct that contains 3 floats (x,y,z)

Every GameObject has a Transform component, The Transform component stores the GameObject's position, rotation and scale.

So in a script you can get an object's position from it's transform.

 public Transform myPlayerTransform;
 void Start() {
     Debug.Log("myPlayerTransform's position is " + myPlayerTransform.position);
 }

$$anonymous$$aybe Unity can explain it better:

http://docs.unity3d.com/$$anonymous$$anual/class-Transform.html

http://docs.unity3d.com/ScriptReference/Transform.html

http://docs.unity3d.com/$$anonymous$$anual/Transforms.html

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.

Follow this Question

Answers Answers and Comments

68 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to add a score in unity? 0 Answers

Anyway To Set A Specific Part Of Code To A Scene? 2 Answers

Scoring different amount of points 0 Answers

Highscore and PlayerPrefs unity C# 1 Answer

Highscore system unity C# 0 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