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
-1
Question by PMPO22 · Apr 15, 2014 at 11:28 PM · android2dgameobjectscore

Score system

I'm programming a simple 2d game for android where you have to kill (destuir) objects that appear on the screen by touching them with your finger, then in my code the score is not working properly because the score increases as had destroyed all the objects in the game, and should be destroyed only assume as one

this is my code

 using UnityEngine;
 
 using System.Collections;
 
 public class AddPoints : MonoBehaviour 
 
 {
     
     void Update () 
 
     {
 
         if (Input.anyKeyDown)
             
         {
              PointScore.pointScore += 1f;
         }
         
     }
     void OnMouseDown(){
     
         Destroy (this.gameObject);
     }   
 public float lifetime = 1.0f;
     void  Awake ()
     {
         Destroy(this, lifetime);
     }
     
     
 
     
 }
 
 
 
 
 
 and
 
 
 
 using UnityEngine;
 
 using System.Collections;
 
 
 
 public class PointScore : MonoBehaviour 
     
 {
     
     public static float pointScore = 0;
     
     
     
     void OnGUI () 
         
     {
         
         GUI.Label (new Rect (10, 10, 100, 20), "Score: " + pointScore);
         
     }
     
 }



if you can help I would be very grateful

Comment
Add comment · Show 1
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 getyour411 · Apr 15, 2014 at 11:53 PM 0
Share

Format this properly, make sure the code you are supplying is relevant to the problem description; make it clear what these scripts are attached to; does On$$anonymous$$ouseDown work for Android?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Pyrian · Apr 16, 2014 at 02:17 AM

You shouldn't be using "Input.anyKeyDown" to update your score. That will return true for EVERY object testing it, for EVERY keystroke or click; thus, your score updates by the number of scoreable objects with every click.

Remove that code, and just put your "PointScore.pointScore += 1f;" in the same OnMouseDown function with your Destroy, and you should be fine.

Comment
Add comment · Show 3 · 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 PMPO22 · Apr 16, 2014 at 03:34 PM 0
Share

Thanks for the answer, but still have a problem because now when I click the object does not increase score or destroys the object

this is the code you recomend

 using UnityEngine;
 
 using System.Collections;
 
 public class AddPoints : $$anonymous$$onoBehaviour 
 
 {
     
     void On$$anonymous$$ouseDown ()
     {
         PointScore.pointScore += 1f;
         Destroy (this.gameObject);
 
     }
 public float lifetime = 1.0f;
     void  Awake ()
     {
         Destroy(this, lifetime);
     }
     
 }

if you have another idea to resolve the problem I would be very grateful

avatar image Pyrian · Apr 16, 2014 at 05:07 PM 0
Share

I didn't realize they weren't being destroyed, either. There are a number of things that can prevent On$$anonymous$$ouseDown from being called, but let's start with the obvious: Does the same GameObject that AddPoints is attached to have an appropriate Collider component that covers its area? Without a collider, On$$anonymous$$ouseDown will never get called.

Also, it might not get called if it's under another object or on the IgnoreRayCast Layer.

avatar image PMPO22 · Apr 17, 2014 at 12:44 AM 0
Share

I have the object with collider and rigidbody 2d and is the default layer

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

21 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

Related Questions

Carry over texts from one scene to another? 1 Answer

OnLevelWasLoaded script problem. 1 Answer

Collision On Specific Object= Destroy not working. 2 Answers

How to form copies of yourself that follow you when you walk? (2D Platformer) 2 Answers

Why won't the Score reset? 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