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 :slight_smile:

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);
    }
}

}

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.

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 :smiley: its increasing if im activating an other panel which i pull in the script in the other panel :slight_smile: TYTYTY :slight_smile: