Hello. i just want to understand how the code below run.
i cant figure out why the return value method called multiple times on runtime or exit runtime.
-
i am using visual studio to edit script
-
code is written in c#
-
using unity for my game developement
this is my example
public static class R1
{
public static int N1;
public static int GetN1()
{
Debug.Log("GetN1");
Debug.Log(N1);
N1++;
Debug.Log(N1);
Return N1;
}
}
public class Tester : Monobehaviour
{
private int A = R1.GetN1();
private void Awake()
{
Debug.Log("Awake");
Debug.Log(A);
}
private void Start()
{
Debug.Log("Start");
Debug.Log(A);
}
private void OnEnable()
{
Debug.Log("Enable");
Debug.Log(A);
}
private void OnDisable()
{
Debug.Log("Disable");
Debug.Log(A);
}
}
when i run the code the outputs are
GetN1
0
1
GetN1
1
2
Awake
2
Enable
2
//when i disable the game object (from unity inspector)
Disable
2
//when i enable the game object
Enable
2
//same goes when i disable or enable the script (Tester)
//but when i exit unity player(means stop running the code in unity)
//the output is
Disable
2
GetN1
2
3
from what i understand i only called GetN1 method once, but from the output it seems to be called
when i start runtime and when i exit runtime.
hope someone can enlighten me.
thank you in advance:)