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 SUPPEAR · Mar 29, 2015 at 10:29 PM · android2dmobile

ScoreManager adding more points than wanted?

Hello. I am using a ScoreManager script that is supposed to add only 1 point every time, the object the script is linked to, is touched. It adds 8 points instead of 1. Why is this happening?

The two scripts are

  using UnityEngine;
  using UnityEngine.UI;
  using System.Collections;
  
  public class ScoreManager : MonoBehaviour
  {
      public static int score;        // The player's score.
      
      
      Text text;                      // Reference to the Text component.
      
      
      void Awake ()
      {
          // Set up the reference.
          text = GetComponent <Text> ();
          
          // Reset the score.
          score = 0;
      }
      
      
      void Update ()
      {
          // Set the displayed text to be the word "Score" followed by the score value.
          text.text = "Score: " + score;
      }
  }
  
  

  using UnityEngine;
  using System.Collections;
  
  public class Expands : MonoBehaviour {
      
      public Transform parent;
      public float expandAmount = 2f;
      public float localPosition = 1f;
      public int scoreValue = 1;
  
      void OnMouseDown()
      {
          parent.localScale *=expandAmount;
          for(int i =0;i<parent.childCount;i++)
          {
              parent.GetChild(i).localScale = parent.GetChild(i).localScale/expandAmount;
              parent.GetChild(i).localPosition += new Vector3(0, 0, -1f);//Added a '+'
              ScoreManager.score += scoreValue;
          }
      }
  }
  
  

The second script is where the points are being directed from. All help is appreciated.

Comment
Add comment · Show 2
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 SUPPEAR · Mar 29, 2015 at 10:52 PM 0
Share

Thank you for the response. You were completely right about the parent having 8 children.

avatar image Huacanacha · Mar 29, 2015 at 10:55 PM 0
Share

Cool. I'll convert to an answer then ;)

Oops... Somehow I deleted that comment. That will $$anonymous$$ch me to try to hit tiny buttons on a phone!

1 Reply

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

Answer by Huacanacha · Mar 29, 2015 at 10:57 PM

Given that you are incrementing the score in a for loop I expect the parent has 8 children. Solution: move the score increment outside the for loop.

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.

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

Transform.localPosition Script Problem. 3 Answers

How to move player using UI buttons, when using Input.mousePosition to shooting? (Android) 0 Answers

2D game shakes,2D Mobile game shaking 1 Answer

Transform.localPosition Scripting Problem. 1 Answer

Collision On Specific Object= Destroy not working. 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