How do you transfer variable values between scripts

So I have a script1 and script2. Script1 contains variable that i want in script2. Now what is the catch here is that variable in script1 is changing constantly but I need this same value that is changing constantly in script 2.

Note that I am new at this and im pretty sure this could be done more efficiently im sure of it so dont judge my code.

My first script is GDL Space - A Space for GDL code.

My second script: GDL Space - A Space for GDL code.

So to explain what im doing. The thing im doing is sprint bar and I have a sprint bar made my own way because im trying to work alone as much as i can to learn something from it. It is made so that its made out of 30 same copy-pasted images like this. Its a very similar bar:

Now I added them to a list and im removing them as soon as the shift + d or shift + a is pressed or adding them back when the shift is not pressed. The script has a inbuilt timer which dictates the bars variable and its constantly changing it. Now as you can see I need this bars value in my first script so i can assign it to the second script on my sprint script which allows me to sprint and I want to only sprint when the value is above 0 which is constantly changing with the custom timer.

Hope I was clear enough. If not I will explain again.

one of your scripts is in c plus plus the other is c sharp

You need to reference the script and your variables need to be public.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player_Movement : MonoBehaviour
{

    public Rigidbody2D playerRB;
    static float x;
    static float y;
    public float speedLRS;

//Add this from the inspector
    [SerializeField] public Sprint_Bar SprintBar;

    public void Update()
    {

        x = Input.GetAxisRaw("Horizontal");
        y = Input.GetAxisRaw("Vertical");
        }
        else if (Input.GetKey(KeyCode.D) && Input.GetKey(KeyCode.LeftShift) && x == 1)
        {
            playerRB.AddForce(Vector2.right * speedLRS);
            //Sprint
        }
        else if (Input.GetKey(KeyCode.A) && Input.GetKey(KeyCode.LeftShift) && x == -1)
        {
            playerRB.AddForce(Vector2.left * speedLRS);
            //Sprint
        }

Now you call SprintBar.barsLess for example from your script to modify it.

You can also make them static variables which will be shared between all your scripts but its not recommended unless you really want global variables

Always the same way:

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

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.

1 Like

You do NOT want to care about that sort of crazy implementation detail in any more than one place.

Steps to success:

  • declare an authoritative integer that is the number of sprint units available, perhaps placing it in a GameManager

  • all of your program should ONLY manipulate that one sprint integer, nothing else

  • have a single tiny script attached to the visible sprint bar sprites. That script:
    —> observes the integer
    —> adjusts the tiles visible