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

Accessing variables in another script

Discussion in 'Scripting' started by naomi_k, Feb 4, 2020.

  1. naomi_k

    naomi_k

    Joined:
    Jan 23, 2020
    Posts:
    24
    Hello,

    I followed this tutorial to make a user interface:



    Basically, when I press the save button I want my user inputs (InputField and dropdown values) to get saved in variables. How do I access these variables (ex name from the video) in another script? This script is non Monobehaviour and I'm using it to output the saved variables into a csv at the end of the game.

    Thanks!
     
  2. mate_veres

    mate_veres

    Joined:
    Oct 19, 2019
    Posts:
    72
    If you want to reach them you should make them public and you have to reference the other script into the script in which you want to access them: this can be done by
    Code (CSharp):
    1. public scriptname name;
    then drag the corresponding script in the inspector.
     
    Last edited: Feb 11, 2020
  3. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,748
    If you're gonna help, it should be correct syntax if nothing else.
    Code (csharp):
    1. //If the other script is named SomeScript
    2. public SomeScript something;
    3.  
    4. //in Update, Start, etc
    5. Debug.Log("A variable's value is "+something.someVariable );
     
  4. j7stin

    j7stin

    Joined:
    Jan 7, 2020
    Posts:
    22
    Couldn't you use PlayerPrefs or some system that stores data in every scene to keep it there forever unless you reset it?
    Something along the lines of

    Code (CSharp):
    1. PlayerPrefs.SetString("userName", name)
    Before that code, ask the user for their name, grab the input and store it in the name variable.
     
  5. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,748
    Let me stop you at that point, because PlayerPrefs is a bad solution. PP is very limited, issue-prone when used in the wrong context, and surprisingly slow.
    LIMITED: You can only store int, string, and float. No references to objects, no data structures, not even Vector3.
    ISSUE-PRONE: If you're not intending for the data to be persistent between runs, will cause all sorts of issues later when your game boots up in an unexpected state because you have leftover data from a previous run.
    SLOW: Let's compare what kind of work the computer has to do each time you access this variable:
    Option A: Using a direct reference (as in my code example from the previous comment): Computer looks at referenced object and reads variable.... and that's literally it.
    Option B: Using PlayerPrefs:
    a) Open the prefs file from hard drive
    b) Parse entire prefs file
    c) Parse the variable from string
    d) return the variable
    If you use PlayerPrefs for all of your exchanges of data between objects, this is actually really really slow. It generates quite a lot of garbage in memory (as just about all parsing does) so this will cause stuttering in your game.

    By the way, because PP doesn't have any support for data structures, it's also not any good for any remotely complicated save game system - like, storing the level you're on and how many points you have sure, but even something like storing positions is just painful in PlayerPrefs, and you should use data serialization as shown in this tutorial (disregard the ancient Unity IMGUI code), which is far faster and, once set up, easier to use than PlayerPrefs.

    What all this boils down to is basically: PlayerPrefs has an accurate name. Use it to store your player's preferences. Any other usage of it is either overkill or not powerful enough. Yes, it's the simplest thing you learned for data persistence in Unity, but that doesn't mean it's good.