Counting Activation of specific panels, not updating Variable?
Hello Guys I wrote following script and add this on a panel. IF this panel gets activated i want to store +1 in my int number variable in the script. Then i activate an other panel with this script on that, but it is giving me the value 1 again. How can i make this number variable updating and saying that now its active 2 panel?
Thank you in Advance :)
using UnityEngine; using System.Collections;
public class RichtigZaehler : MonoBehaviour {
public GameObject panel;
private int number;
// Use this for initialization
void Start () {
counterMethod();
}
public void counterMethod()
{
if (panel.activeSelf)
{
number = number + 1;
print("Number of Activated Panels= "+number);
}
}
}
Answer by KoenigX3 · Jan 15, 2017 at 05:23 PM
The 'number' variable should be static, so it will be shared across all instances. If the A panel is activated, the B panel should receive the updated number as well.
However, if you are activating the panels with GameObject.SetActive(), and you want to measure every activation, you have to rewrite your code. Right now, it will only check the active state once (at the Start()), and it will increase the number by 1.
When you call GameObject.SetActive() on a panel, you should always call a static method on the panel script, which will increase the activation number by 1.
Answer by ripane · Jan 15, 2017 at 05:43 PM
Hey Koenigx3,
ty for your fast answer. Yes im setting panels active with the method SetActive(True).
After i made the Number Variable Static it worked :D its increasing if im activating an other panel which i pull in the script in the other panel :) TYTYTY :)
Your answer
![](https://koobas.hobune.stream/wayback/20220612104850im_/https://answers.unity.com/themes/thub/images/avi.jpg)