Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

how to call a variables value from another script without making it a static variable?

Discussion in 'Getting Started' started by Sylvir, Apr 23, 2015.

  1. Sylvir

    Sylvir

    Joined:
    Mar 21, 2015
    Posts:
    396
    not even really sure where to start looking in the documentation for this, i was told earlier in the forums here that i didnt need to use a static variable to get the value from another script. it is telling me in my unity program though that i can not access public variables from another script unless they are static. I have a situation in my program where i can't make the value static because i have it on many buttons. I then used the editor to manually set the values for each button using the same script. When it comes to saving those values sepratly though its causing issues (not letting me access that variable because its not static etc) any ideas how i can do this without making a static variable? thanks
     
  2. CodeMonke234

    CodeMonke234

    Joined:
    Oct 13, 2010
    Posts:
    181
    best to post code and errors.

    To set variable
    GetComponent<SomeOtherScript>().public_variable=0;
     
  3. Sylvir

    Sylvir

    Joined:
    Mar 21, 2015
    Posts:
    396
    ah, okay thanks! that is probably all i was looking for, if there is more issues ill post a code snipit. thanks! ill check into it :)
     
  4. Sylvir

    Sylvir

    Joined:
    Mar 21, 2015
    Posts:
    396
    just trying to find a way to save things using playerprefs for now. before i release the project to anyone to play ill go through and serialize the save info. Just using the playerprefs saves now to test and balance the project.
     
  5. jhocking

    jhocking

    Joined:
    Nov 21, 2009
    Posts:
    813
    Basically, static variables are accessed through the script itself, while normal public variables are attached to instances of the script (ie. a reference that is active in the scene) and what GetComponent() is doing is getting you an instance of the script. You may want to look up some basic information about object-oriented programming.
     
    Kiwasi and Sylvir like this.