- Home /
Stop Score From Adding?
How can I stop my score from adding temporally for a certain amount of seconds? My score system uses Time, but I've had too many problems with using Time.timeScale = 0, so I would like an answer that doesn't involve that(unless it's the only way). So how can I do this? EDIT: I've made changes to the script, the new one posted is not the same as it was before. Just a heads up to the people who read this before. Here's my(New) script:
float playerScore = 0;
public Font MyFont;
public bool On = true;
public bool Off = false;
ControllerScript player;
void Start()
{
player = GetComponent<ControllerScript>();
}
void Update () {
if(On == true && Off == false)
playerScore += Time.deltaTime;
}
public void IncreaseScore(int amount)
{
playerScore += amount;
}
void OnTriggerEnter2D(Collider2D col)
{
// If the colliding gameobject is Object...
if(col.gameObject.tag == "Item1")
{
On = false;
Off = true;
}
}
void OnDisable()
{
PlayerPrefs.SetInt ("Score", (int)(playerScore * 5));
PlayerPrefs.SetString("PlayerName", "High Score");
PlayerPrefs.Save ();
Debug.Log("Saved, Yes!");
}
void OnGUI()
{
GUI.skin.font = MyFont;
GUI.contentColor = Color.white;
GUI.Label (new Rect(10, 10, 200, 30), "Score: " + (int)(playerScore * 5));
}
}
All I really need to know is how to stop it, I can manage the seconds part.
just use boolean val, switch it on/off when you need e.g if (addpoints) points++;
Yeah this could work. I'll mess around and see what I can do.
Answer by christoph_r · Aug 12, 2014 at 01:18 AM
There are numerous ways to do this. One would be to create a bool that needs to be true so the script adds to the score. Then create a method that simply sets that bool to true. Now if you want the scoring to stop, set the bool to false and invoke (one single time! So not in Update
, or at least check if that bool isn't already true) the method that sets the bool to true again. When you invoke, you can set the amount of seconds you want to wait before the scoring continues again.
Another way would be using coroutines, but that requires some more reading to get into. It's a good idea to look at coroutines sooner or later, though, because they can be really powerful to use.
The scoring needs to stop upon collision or the not-scoring? If the first is the case, you can simply call the methd you created for temporarily stopping the scoring from another script with an OnTriggerEnter
(or a similar) method. If it's the latter you're after, ins$$anonymous$$d of calling the wait method, you simply set the bool for scoring to true and cancel the invoke.
Okay, am I getting this right: The scoring continues again IF the player collides with the object again OR if the 3 seconds run out? If so: create the bool (I'll call it scoring
for this post) as I said. Then create a method ResetBool that does nothing else except setting scoring
to true. Then, in your`IncreaseScore` method, check if scoring
, and only then add to the score.
In your OnTriggerEnter method, check if scoring
is true. If
so, set it to false and call Invoke("ResetBool", 3f);
. Ins$$anonymous$$d of 3f, you can also use a variable resetTime, of course. In the corresponding else
statement (that handles the case when the scoring already stopped and the player collides again), set the bool back to true
and call CancelInvoke("ResetBool");
I hope that does the trick.
Cool. Glad you managed to figure that out on your own, bug tracking can be tricky business! In that case, make sure you've set everything up, that both objects have colliders and at least one is a trigger. The documentation also has a useful chart showing when OnTriggerEnter works.
That depends on the setup of your objects. Here is a nice overview about what method will be called in which situations.