How can I use a same public integer in two different scripts?

I defined a public int in a script

public int onEvent = 0;

and then I tried to use it directly in another script file in a code like:

if(Input.GetKeyDown(KeyCode.Return) && onEvent == 0){...}

It didn’t work. I defined another int in the second script file with the same name, but it instead created two different ints with same names.

What do I have to do to call the same integer from a different script file?

You need to pass the object reference or make it a publicly accessible static variable.

1 Like

Hi, thank you for your answer.
I changed my first code to public static int onEvent = 0;
Is it enough to make it a publicly accessible static variable?
and can you please tell me what do you mean by “passing the object reference”, I am a beginner.