How Do I Get Variable Values From Another Script?

I have two scripts. One is GameManager.cs and the other is vThirdPersonController.cs. The vThirdPersonController.cs script is part of the free Invector 3rd Person Controller asset from the asset store, but I have been modifying it as I go. In my GameManager script, I’m trying to reference the “goalCompleted” variable in controller script and keep getting the error “The name ‘vThirdPersonController’ does not exist in the current context”. What am I doing wrong here?

GameManager.cs - Line 15 is where I referenced it.

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

public class GameManager : MonoBehaviour
{

    private float elapsedTime = 0;
    private bool isRunning = false;
    private bool isFinished = false;
    public Text timerText;
    public GameObject goal;
    public GameObject player;
    bool goalReached = vThirdPersonController.goalCompleted;

    // Start is called before the first frame update
    void Start()
    {
        isRunning = true;
        isFinished = false;
        elapsedTime = 0;
       
    }

    // Update is called once per frame
    void Update()
    {
        // Add time to the clock if the game is running
        if (isRunning)
        {
            elapsedTime = elapsedTime + Time.deltaTime;
            timerText.text = elapsedTime.ToString();
           
        } else if (goalReached == true)
        {
            Debug.Log("Level Complete. Time was: " + elapsedTime.ToString());
        }
       
    }

    private void StartGame()
    {
        elapsedTime = 0;
        isRunning = true;
        isFinished = false;
    }

    public void FinishedGame()
    {
       
    }

   
}

vThirdPersonController.cs - Line 15 is the variable value I’m trying to get.

using UnityEngine;

namespace Invector.vCharacterController
{
    public class vThirdPersonController : vThirdPersonAnimator
    {
       
        public AudioSource audioSource;
        public AudioClip confirmed;
        public AudioClip denied;
        public AudioClip printerPickup;
        public AudioClip jump;
        public AudioClip walk;
        public AudioClip goal;
        public static bool goalCompleted = false;
        public bool printerCollected;
        public GameObject printer;
        public GameObject door;
        public float doorSpeed;
        public Collider doorCollider;


        void start ()
        {
            audioSource = GetComponent<AudioSource>();
            doorCollider = door.GetComponent<Collider>();

        }

        public virtual void ControlAnimatorRootMotion()
        {
            if (!this.enabled) return;

            if (inputSmooth == Vector3.zero)
            {
                transform.position = animator.rootPosition;
                transform.rotation = animator.rootRotation;
            }

            if (useRootMotion)
                MoveCharacter(moveDirection);
        }

        public virtual void ControlLocomotionType()
        {
            if (lockMovement) return;

            if (locomotionType.Equals(LocomotionType.FreeWithStrafe) && !isStrafing || locomotionType.Equals(LocomotionType.OnlyFree))
            {
                SetControllerMoveSpeed(freeSpeed);
                SetAnimatorMoveSpeed(freeSpeed);
            }
            else if (locomotionType.Equals(LocomotionType.OnlyStrafe) || locomotionType.Equals(LocomotionType.FreeWithStrafe) && isStrafing)
            {
                isStrafing = true;
                SetControllerMoveSpeed(strafeSpeed);
                SetAnimatorMoveSpeed(strafeSpeed);
            }

            if (!useRootMotion)
                MoveCharacter(moveDirection);
        }

        public virtual void ControlRotationType()
        {
            if (lockRotation) return;

            bool validInput = input != Vector3.zero || (isStrafing ? strafeSpeed.rotateWithCamera : freeSpeed.rotateWithCamera);

            if (validInput)
            {
                // calculate input smooth
                inputSmooth = Vector3.Lerp(inputSmooth, input, (isStrafing ? strafeSpeed.movementSmooth : freeSpeed.movementSmooth) * Time.deltaTime);

                Vector3 dir = (isStrafing && (!isSprinting || sprintOnlyFree == false) || (freeSpeed.rotateWithCamera && input == Vector3.zero)) && rotateTarget ? rotateTarget.forward : moveDirection;
                RotateToDirection(dir);
            }
        }

        public virtual void UpdateMoveDirection(Transform referenceTransform = null)
        {
            if (input.magnitude <= 0.01)
            {
                moveDirection = Vector3.Lerp(moveDirection, Vector3.zero, (isStrafing ? strafeSpeed.movementSmooth : freeSpeed.movementSmooth) * Time.deltaTime);
                return;
            }

            if (referenceTransform && !rotateByWorld)
            {
                //get the right-facing direction of the referenceTransform
                var right = referenceTransform.right;
                right.y = 0;
                //get the forward direction relative to referenceTransform Right
                var forward = Quaternion.AngleAxis(-90, Vector3.up) * right;
                // determine the direction the player will face based on input and the referenceTransform's right and forward directions
                moveDirection = (inputSmooth.x * right) + (inputSmooth.z * forward);
            }
            else
            {
                moveDirection = new Vector3(inputSmooth.x, 0, inputSmooth.z);
            }
        }

        public virtual void Sprint(bool value)
        {
            var sprintConditions = (input.sqrMagnitude > 0.1f && isGrounded &&
                !(isStrafing && !strafeSpeed.walkByDefault && (horizontalSpeed >= 0.5 || horizontalSpeed <= -0.5 || verticalSpeed <= 0.1f)));

            if (value && sprintConditions)
            {
                if (input.sqrMagnitude > 0.1f)
                {
                    if (isGrounded && useContinuousSprint)
                    {
                        isSprinting = !isSprinting;
                    }
                    else if (!isSprinting)
                    {
                        isSprinting = true;
                    }
                }
                else if (!useContinuousSprint && isSprinting)
                {
                    isSprinting = false;
                }
            }
            else if (isSprinting)
            {
                isSprinting = false;
            }
        }

        public virtual void Strafe()
        {
            isStrafing = !isStrafing;
        }

        public virtual void Jump()
        {
            // trigger jump behaviour
            jumpCounter = jumpTimer;
            isJumping = true;

            audioSource.clip = jump;
            audioSource.Play();

            // trigger jump animations
            if (input.sqrMagnitude < 0.1f)
                animator.CrossFadeInFixedTime("Jump", 0.1f);
            else
                animator.CrossFadeInFixedTime("JumpMove", .2f);
        }

        void OnCollisionEnter(Collision collision)
        {
            if (collision.gameObject.tag == "Door")
            {
                if (printerCollected == true)
                {
                    Debug.Log("Door opened");
                    audioSource.clip = confirmed;
                    audioSource.Play();
                    doorCollider.enabled = !doorCollider.enabled;
                    door.transform.position = new Vector3(-0.986999989f,1.05842531f,0f);
                } else
                {
                    Debug.Log("Can't open");
                    audioSource.clip = denied;
                    audioSource.Play();
                   
                }
               
            }

            if (collision.gameObject.tag == "Printer")
            {
                Debug.Log("Printer collected.");
                Destroy(printer);
                printerCollected = true;
                audioSource.clip = printerPickup;
                audioSource.Play();
            }

            if (collision.gameObject.tag == "Goal")
            {
                Debug.Log("Goal reached");
                audioSource.clip = goal;
                audioSource.Play();
                goalCompleted = true;
               
            }

           
        }

    }
}

It’s in a namespace, so you just need using Invector.vCharacterController; at the top of your script.

Mind you, it’s value won’t update as the goalCompleted value changes, so there’s no point of assigning it to a field in your manager script. You’re better off just reading the static value whenever you need it.

1 Like

I can’t believe the answer was that easy. lol - Makes me embarrassed to say how long I was trying to figure it out. Apparently I have to do some research on Namespaces because I originally had tried adding the class from the controller script to the manager script. So I was close… but not really. Also thank you for pointing out the part about the static value, definitely a good call.