Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

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

Discussion in 'Scripting' started by CelilOguz, Apr 30, 2020.

  1. CelilOguz

    CelilOguz

    Joined:
    Apr 30, 2020
    Posts:
    3
    I defined a public int in a script

    Code (CSharp):
    1. public int onEvent = 0;
    and then I tried to use it directly in another script file in a code like:
    Code (CSharp):
    1. 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?
     
    Last edited: Apr 30, 2020
  2. matkoniecz

    matkoniecz

    Joined:
    Feb 23, 2020
    Posts:
    170
    You need to pass the object reference or make it a publicly accessible static variable.
     
    CelilOguz likes this.
  3. CelilOguz

    CelilOguz

    Joined:
    Apr 30, 2020
    Posts:
    3
    Hi, thank you for your answer.
    I changed my first code to
    Code (CSharp):
    1. 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.