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

Resolved How Do I Get Variable Values From Another Script?

Discussion in 'Scripting' started by Duetschbag, Apr 6, 2023.

  1. Duetschbag

    Duetschbag

    Joined:
    Jun 12, 2014
    Posts:
    10
    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.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class GameManager : MonoBehaviour
    7. {
    8.  
    9.     private float elapsedTime = 0;
    10.     private bool isRunning = false;
    11.     private bool isFinished = false;
    12.     public Text timerText;
    13.     public GameObject goal;
    14.     public GameObject player;
    15.     bool goalReached = vThirdPersonController.goalCompleted;
    16.  
    17.     // Start is called before the first frame update
    18.     void Start()
    19.     {
    20.         isRunning = true;
    21.         isFinished = false;
    22.         elapsedTime = 0;
    23.        
    24.     }
    25.  
    26.     // Update is called once per frame
    27.     void Update()
    28.     {
    29.         // Add time to the clock if the game is running
    30.         if (isRunning)
    31.         {
    32.             elapsedTime = elapsedTime + Time.deltaTime;
    33.             timerText.text = elapsedTime.ToString();
    34.            
    35.         } else if (goalReached == true)
    36.         {
    37.             Debug.Log("Level Complete. Time was: " + elapsedTime.ToString());
    38.         }
    39.        
    40.     }
    41.  
    42.     private void StartGame()
    43.     {
    44.         elapsedTime = 0;
    45.         isRunning = true;
    46.         isFinished = false;
    47.     }
    48.  
    49.     public void FinishedGame()
    50.     {
    51.        
    52.     }
    53.  
    54.    
    55. }
    56.  
    vThirdPersonController.cs - Line 15 is the variable value I'm trying to get.
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. namespace Invector.vCharacterController
    4. {
    5.     public class vThirdPersonController : vThirdPersonAnimator
    6.     {
    7.        
    8.         public AudioSource audioSource;
    9.         public AudioClip confirmed;
    10.         public AudioClip denied;
    11.         public AudioClip printerPickup;
    12.         public AudioClip jump;
    13.         public AudioClip walk;
    14.         public AudioClip goal;
    15.         public static bool goalCompleted = false;
    16.         public bool printerCollected;
    17.         public GameObject printer;
    18.         public GameObject door;
    19.         public float doorSpeed;
    20.         public Collider doorCollider;
    21.  
    22.  
    23.         void start ()
    24.         {
    25.             audioSource = GetComponent<AudioSource>();
    26.             doorCollider = door.GetComponent<Collider>();
    27.  
    28.         }
    29.  
    30.         public virtual void ControlAnimatorRootMotion()
    31.         {
    32.             if (!this.enabled) return;
    33.  
    34.             if (inputSmooth == Vector3.zero)
    35.             {
    36.                 transform.position = animator.rootPosition;
    37.                 transform.rotation = animator.rootRotation;
    38.             }
    39.  
    40.             if (useRootMotion)
    41.                 MoveCharacter(moveDirection);
    42.         }
    43.  
    44.         public virtual void ControlLocomotionType()
    45.         {
    46.             if (lockMovement) return;
    47.  
    48.             if (locomotionType.Equals(LocomotionType.FreeWithStrafe) && !isStrafing || locomotionType.Equals(LocomotionType.OnlyFree))
    49.             {
    50.                 SetControllerMoveSpeed(freeSpeed);
    51.                 SetAnimatorMoveSpeed(freeSpeed);
    52.             }
    53.             else if (locomotionType.Equals(LocomotionType.OnlyStrafe) || locomotionType.Equals(LocomotionType.FreeWithStrafe) && isStrafing)
    54.             {
    55.                 isStrafing = true;
    56.                 SetControllerMoveSpeed(strafeSpeed);
    57.                 SetAnimatorMoveSpeed(strafeSpeed);
    58.             }
    59.  
    60.             if (!useRootMotion)
    61.                 MoveCharacter(moveDirection);
    62.         }
    63.  
    64.         public virtual void ControlRotationType()
    65.         {
    66.             if (lockRotation) return;
    67.  
    68.             bool validInput = input != Vector3.zero || (isStrafing ? strafeSpeed.rotateWithCamera : freeSpeed.rotateWithCamera);
    69.  
    70.             if (validInput)
    71.             {
    72.                 // calculate input smooth
    73.                 inputSmooth = Vector3.Lerp(inputSmooth, input, (isStrafing ? strafeSpeed.movementSmooth : freeSpeed.movementSmooth) * Time.deltaTime);
    74.  
    75.                 Vector3 dir = (isStrafing && (!isSprinting || sprintOnlyFree == false) || (freeSpeed.rotateWithCamera && input == Vector3.zero)) && rotateTarget ? rotateTarget.forward : moveDirection;
    76.                 RotateToDirection(dir);
    77.             }
    78.         }
    79.  
    80.         public virtual void UpdateMoveDirection(Transform referenceTransform = null)
    81.         {
    82.             if (input.magnitude <= 0.01)
    83.             {
    84.                 moveDirection = Vector3.Lerp(moveDirection, Vector3.zero, (isStrafing ? strafeSpeed.movementSmooth : freeSpeed.movementSmooth) * Time.deltaTime);
    85.                 return;
    86.             }
    87.  
    88.             if (referenceTransform && !rotateByWorld)
    89.             {
    90.                 //get the right-facing direction of the referenceTransform
    91.                 var right = referenceTransform.right;
    92.                 right.y = 0;
    93.                 //get the forward direction relative to referenceTransform Right
    94.                 var forward = Quaternion.AngleAxis(-90, Vector3.up) * right;
    95.                 // determine the direction the player will face based on input and the referenceTransform's right and forward directions
    96.                 moveDirection = (inputSmooth.x * right) + (inputSmooth.z * forward);
    97.             }
    98.             else
    99.             {
    100.                 moveDirection = new Vector3(inputSmooth.x, 0, inputSmooth.z);
    101.             }
    102.         }
    103.  
    104.         public virtual void Sprint(bool value)
    105.         {
    106.             var sprintConditions = (input.sqrMagnitude > 0.1f && isGrounded &&
    107.                 !(isStrafing && !strafeSpeed.walkByDefault && (horizontalSpeed >= 0.5 || horizontalSpeed <= -0.5 || verticalSpeed <= 0.1f)));
    108.  
    109.             if (value && sprintConditions)
    110.             {
    111.                 if (input.sqrMagnitude > 0.1f)
    112.                 {
    113.                     if (isGrounded && useContinuousSprint)
    114.                     {
    115.                         isSprinting = !isSprinting;
    116.                     }
    117.                     else if (!isSprinting)
    118.                     {
    119.                         isSprinting = true;
    120.                     }
    121.                 }
    122.                 else if (!useContinuousSprint && isSprinting)
    123.                 {
    124.                     isSprinting = false;
    125.                 }
    126.             }
    127.             else if (isSprinting)
    128.             {
    129.                 isSprinting = false;
    130.             }
    131.         }
    132.  
    133.         public virtual void Strafe()
    134.         {
    135.             isStrafing = !isStrafing;
    136.         }
    137.  
    138.         public virtual void Jump()
    139.         {
    140.             // trigger jump behaviour
    141.             jumpCounter = jumpTimer;
    142.             isJumping = true;
    143.  
    144.             audioSource.clip = jump;
    145.             audioSource.Play();
    146.  
    147.             // trigger jump animations
    148.             if (input.sqrMagnitude < 0.1f)
    149.                 animator.CrossFadeInFixedTime("Jump", 0.1f);
    150.             else
    151.                 animator.CrossFadeInFixedTime("JumpMove", .2f);
    152.         }
    153.  
    154.         void OnCollisionEnter(Collision collision)
    155.         {
    156.             if (collision.gameObject.tag == "Door")
    157.             {
    158.                 if (printerCollected == true)
    159.                 {
    160.                     Debug.Log("Door opened");
    161.                     audioSource.clip = confirmed;
    162.                     audioSource.Play();
    163.                     doorCollider.enabled = !doorCollider.enabled;
    164.                     door.transform.position = new Vector3(-0.986999989f,1.05842531f,0f);
    165.                 } else
    166.                 {
    167.                     Debug.Log("Can't open");
    168.                     audioSource.clip = denied;
    169.                     audioSource.Play();
    170.                    
    171.                 }
    172.                
    173.             }
    174.  
    175.             if (collision.gameObject.tag == "Printer")
    176.             {
    177.                 Debug.Log("Printer collected.");
    178.                 Destroy(printer);
    179.                 printerCollected = true;
    180.                 audioSource.clip = printerPickup;
    181.                 audioSource.Play();
    182.             }
    183.  
    184.             if (collision.gameObject.tag == "Goal")
    185.             {
    186.                 Debug.Log("Goal reached");
    187.                 audioSource.clip = goal;
    188.                 audioSource.Play();
    189.                 goalCompleted = true;
    190.                
    191.             }
    192.  
    193.            
    194.         }
    195.  
    196.     }
    197. }
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,015
    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.
     
    Duetschbag likes this.
  3. Duetschbag

    Duetschbag

    Joined:
    Jun 12, 2014
    Posts:
    10
    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.