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. Dismiss Notice

Question Trying to pass variables from one script to another

Discussion in 'Scripting' started by SinfulBasilisk, May 11, 2023.

  1. SinfulBasilisk

    SinfulBasilisk

    Joined:
    Jul 12, 2021
    Posts:
    3
    Hello, I am brand new to C#, and I'm trying to pass variables in an array from one script to another. I have a script that does make the array, but I'm having trouble with getting the information out of it. Here is the setup script:

    Code (CSharp):
    1. using System.Collections.Concurrent;
    2. using System.Web;
    3. using UnityEngine;
    4. using static LocationManager;
    5.  
    6.  
    7. public class LocationManager : MonoBehaviour
    8. {
    9.     // Define a class for the item
    10.     [System.Serializable]
    11.     public class Location
    12.     {
    13.         public string locationName;
    14.         public int locationNumber;
    15.         public float locationSuccessRate;
    16.         public float locationWorkTime;
    17.     }
    18.  
    19.     // Array of items
    20.     public Location[] locations;
    21.  
    22.     // Function to retrieve item variables based on the item name
    23.     public void GetLocationVariables(string locationName)
    24.     {
    25.         foreach (Location location in locations)
    26.         {
    27.             if (location.locationName == locationName)
    28.             {
    29.                 currentName = location.locationName;
    30.                 currentNumber = location.locationNumber;
    31.                 currentSuccessRate = location.locationSuccessRate;
    32.                 currentWorkTime = location.locationWorkTime;
    33.                 Debug.Log("Location Name: " + location.locationName);
    34.                 Debug.Log("Location Number: " + location.locationNumber);
    35.                 Debug.Log("Location Success Chance: " + location.locationSuccessRate);
    36.                 Debug.Log("Location Time to Work: " + location.locationWorkTime);
    37.                 break;
    38.             }
    39.         }
    40.     }
    41. }
    42.  
    And then I access it with this:


    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using TMPro;
    4.  
    5. public class ItemWorker : MonoBehaviour
    6. {
    7.     public TextMeshProUGUI itemText;
    8.     private int itemCount;
    9.     private float timer;
    10.  
    11.     public LocationManager functionScriptReference;
    12.  
    13.     private void Start()
    14.     {
    15.         itemCount = 0;
    16.         timer = 1f;
    17.         functionScriptReference.GetLocationVariables
    18.     }
    19.  
    20. ...

    But Unity is saying LocationManager.cs(29,17): error CS0103: The name 'currentName' does not exist in the current context, with all the variables I'm trying to pull. I'm probably doing something simple wrong, but like I said this is my very first time using C#.

    Thanks!
     
  2. Flynn_Prime

    Flynn_Prime

    Joined:
    Apr 26, 2017
    Posts:
    383
    You haven't defined the variables currentName, currentNumber, currentSuccessRate or currentWorkTime. This is how you should define them as local variables (only the method they are defined them can access them). If other methods or scripts need to access them then declare them as fields with the other variables at the top of your script.

    Code (CSharp):
    1.             if (location.locationName == locationName)
    2.             {
    3.                 string currentName = location.locationName;
    4.                 int currentNumber = location.locationNumber;
    5.                 float currentSuccessRate = location.locationSuccessRate;
    6.                 float currentWorkTime = location.locationWorkTime;
    7.                 Debug.Log("Location Name: " + location.locationName);
    8.                 Debug.Log("Location Number: " + location.locationNumber);
    9.                 Debug.Log("Location Success Chance: " + location.locationSuccessRate);
    10.                 Debug.Log("Location Time to Work: " + location.locationWorkTime);
    11.                 break;
    12.             }
    I also notice that these variables aren't actually being used, but I'm unsure if this is because the code is just a snippet or not.
     
    seejayjames likes this.
  3. SinfulBasilisk

    SinfulBasilisk

    Joined:
    Jul 12, 2021
    Posts:
    3

    Thank you, that's what it was missing. The code calling it is just a snippet of the code since I haven't finished writing that.
     
    Flynn_Prime likes this.
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,710
    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.
     
    Ryiah likes this.