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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Question Can't get variable from another script

Discussion in 'Scripting' started by gabbyraine125, Jan 24, 2023.

  1. gabbyraine125

    gabbyraine125

    Joined:
    Jan 23, 2023
    Posts:
    2
    Basically, I keep getting these errors:

    FullName.cs(18,43): error CS0103: The name 'firstName' does not exist in the current context
    FullName.cs(18,43): error CS0103: The name 'lastName' does not exist in the current context

    When I try to do this:

    public class FullName : MonoBehaviour
    {

    public FirstNameInfo secondScript;
    public LastNameInfo thirdScript;
    public TMP_Text fullname;

    public void Start()
    {

    fullname.text = firstName + " " + lastName;

    }


    }

    I'm sure I haven't misspelled anything, so I think it's something with the way I scripted the FirstName and LastName files? I can put the FirstName file here (LastName is essentially the same thing: )

    public class FirstNameInfo : MonoBehaviour
    {
    public Button btnClick;

    public TMP_InputField inputUser;

    private void Start()
    {
    btnClick.onClick.AddListener(FirstNameInfoHandler);
    }

    public void setName()
    {
    string firstName = inputUser.text;
    }

    public void FirstNameInfoHandler()
    {
    Debug.Log("Log input: " + inputUser.text);
    }

    }

    And that's basically it. I followed a bunch of tutorials, but eh it still gives me errors. I'm super new to coding so I'm not even entirely sure if the way I'm doing this is a -good- way, but I just want to make it work... thanks in advance
     
  2. SF_FrankvHoof

    SF_FrankvHoof

    Joined:
    Apr 1, 2022
    Posts:
    780
    Please use Code-Tags when posting code. It makes it a lot easier to read.

    The reason you're getting these errors is because 'firstName' and 'lastName' do not exist in the 'FullName'-class.

    Have a look at the code below. It's probably fairly close to what you're attempting to do.

    Code (CSharp):
    1. public class FullName : MonoBehaviour
    2. {
    3.     public FirstNameInfo secondScript;
    4.     public TMP_Text fullname;
    5.  
    6.     public void Start()
    7.     {
    8.         fullname.text = secondScript.FirstName; // Refer to the FirstName-Variable inside the FirstNameInfo-class
    9.     }
    10. }
    11.  
    12. public class FirstNameInfo : MonoBehaviour
    13. {
    14.     public Button btnClick;
    15.     public TMP_InputField inputUser;
    16.     public string FirstName; // Create a variable called FirstName inside this class
    17.  
    18.     private void Start()
    19.     {
    20.         btnClick.onClick.AddListener(FirstNameInfoHandler);
    21.     }
    22.  
    23.     public void setName()
    24.     {
    25.         FirstName = inputUser.text; // Set the value of the FirstName-variable from a UI-InputField
    26.     }
    27.  
    28.     public void FirstNameInfoHandler()
    29.     {
    30.         Debug.Log("Log input: " + inputUser.text);
    31.         setName();
    32.     }
    33. }
    34.  
     
    gabbyraine125 likes this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,946
    As noted above, if you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/

    Referencing variables, fields, methods (anything non-static) in other script instances:

    https://forum.unity.com/threads/hel...-vars-in-another-script.1076825/#post-6944639

    https://forum.unity.com/threads/accessing-a-gameobject-in-different-scene.1103239/

    REMEMBER: it isn't always the best idea for everything to access everything else all over the place. For instance, it is BAD for the player to reach into an enemy and reduce his health.

    Instead there should be a function you call on the enemy to reduce his health. All the same rules apply for the above steps: the function must be public AND you need a reference to the class instance.

    That way the enemy (and only the enemy) has code to reduce his health and simultaneously do anything else, such as kill him or make him reel from the impact, and all that code is centralized in one place.
     
    gabbyraine125 likes this.
  4. gabbyraine125

    gabbyraine125

    Joined:
    Jan 23, 2023
    Posts:
    2
    Thanks so much! There are no more errors now, but I had a problem getting the text to show up in game for a while. too. Somehow, changing "Start" into "Update" in the FullName file finally fixed that too. Thank you again