- Home /
Point Counter Works Only Once!
Hello guys. I pretty much have many canvas in my scene. What I want is when a canvas is enabled to add 1 point. Here is the script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class test2 : MonoBehaviour
{
public Text scoreText;
public int point;
private bool shouldScore = true;
void OnEnable ()
{
if (this.shouldScore && gameObject.activeSelf)
{
point++;
scoreText.text = point + "";
shouldScore = false;
}
}
}
So it works only for the 1st canvas. One guy told me to modify shouldScore = false; but i can not figure it out.. any thoughts?
Answer by tormentoarmagedoom · Sep 15, 2018 at 06:14 PM
Good day.
So many "strange" things. You are little lost. I supose this script is attached to the canvas itself.
first, in the method OnEnable, is unnessary use the condition gameObject.activeSelf, because this methid is runned the first frame the object becomes active, so always will be true.
Second, use this.shouldScore is the same as shouldScore , its a private variable, only exists in this script. in this gameobject, so the comand this is implicit.
Then.
If this script is in every canvas, every canvas is storing only its own puntuation. Each canvas, when open change its poit value from 0 to 1 , thats all.
You need a general script to store the puntuation, and each canvas script must change the value of points from this general script, not from its own.
If you dont know how to do this, you need to learn basic code about reference other scripts. is 100% basic if you want to continue developing.
Good luck! Bye!
Your answer
Follow this Question
Related Questions
how can I change a light with multiple triggers. ? 0 Answers
How to lock an int as a playerPref? 1 Answer
Trying to find the highest number than add it to itself. 2 Answers
How do I make do I make a variable go back to its original value for a spell system 1 Answer
how to allow the key to only open 1 door rather than all of them? 0 Answers