Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by kyly1992 · May 28, 2014 at 06:42 PM · positionscorecoordinatespointsadding

How do i add points when the object past a specific coordinate

 using UnityEngine;
 using System.Collections;
 
 public class score : MonoBehaviour {
     private int scoreValue;
     public GUIText guiScore;
     private Transform Obstacle;
 
 
     // Use this for initialization
     void Start () {
         Obstacle = transform;
         scoreValue = 0;
         guiScore.text = "Score: 0";
         
     
     }
 
     // Update is called once per frame
         void Update () {
         if (Obstacle.position.x <= -19.2 ){
             scoreValue += 10;
             guiScore.text = "Score: " + scoreValue;





this is what i have so far, it adds the points but instead of adding 10 everytime one of the obstacles past the x coordindate it keeps on adding ( think its becuz of the "<") so i need help in only adding 10 when it "equals" that specific coordinate

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
3
Best Answer

Answer by Chris_Dlala · May 28, 2014 at 07:49 PM

Hey, your condition in the update will be true every frame that that condition is met. If you only want the condition to be true once you need an additional check or to move / destroy the obstacle.

 bool hasAwardedPoints = false;
 void Update ()
 {
      if (Obstacle.position.x <= -19.2f  && !hasAwardedPoints)
      {
          scoreValue += 10;
          guiScore.text = "Score: " + scoreValue;
          hasAwardedPoints = true;
      }
 }

or

 void Update ()
 {
      if (Obstacle && Obstacle.position.x <= -19.2f)
      {
          scoreValue += 10;
          guiScore.text = "Score: " + scoreValue;
          Destroy(Obstacle.gameObject);
      }
 }

I hope that helps =)

Comment
Add comment · Show 6 · 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 kyly1992 · May 29, 2014 at 02:41 PM 0
Share

thank you chris it worked :)

avatar image Chris_Dlala · May 29, 2014 at 03:12 PM 0
Share

Glad I could help! Could you accept my answer please. =)

avatar image kyly1992 · May 29, 2014 at 04:32 PM 0
Share

ok im having a problem i created couple more obstacles, and when i attach the script to them ins$$anonymous$$d of adding to the score it starts it over from 10, how can i make it add to the already existing score ins$$anonymous$$d of restarting?

avatar image Chris_Dlala · May 30, 2014 at 08:39 AM 0
Share

You don't really want each component as Score managing one Obstacle. It would be better to have just one score component (maybe a singleton) to access score. A quick fix would be to make the score variable static and not reset the score to 0 in start.

avatar image kyly1992 · May 30, 2014 at 02:50 PM 0
Share

THAN$$anonymous$$ YOUUUU making the variable static truly help, thankssss againnnn, was fighting with this for a couple days, still a unity newbie, thankss

Show more comments

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

22 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

Related Questions

Transform vector to other coordinate frame using quaternions 1 Answer

open door to next level, after 10000 points were added to your score (solved) 2 Answers

Highscore Save 0 Answers

Is gameObject present at coordinates (x,y,z)? 1 Answer

Having trouble updating the text in my GUI 2 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