- Home /
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.
Thank you for the response. You were completely right about the parent having 8 children.
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!
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.
Your answer
Follow this Question
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