Resolving CS0103: The name 'currentSpeed' does not exist in the current context
The C# compiler error CS0103 indicates that your script is attempting to read, write, or evaluate a variable named currentSpeed that has either not been declared, is spelled with different capitalization, or exists in a different variable scope (such as being declared locally inside a method rather than as a class-level member variable).
Variable Scope Rules in Unity C#
If you declare float currentSpeed = 50f; inside the Start() or Awake() method, that variable is local to that method's execution stack and is immediately destroyed once Start() exits. Attempting to access currentSpeed inside Update() or FixedUpdate() will trigger compiler error CS0103.
using UnityEngine;
public class AircraftSpeedController : MonoBehaviour
{
[Header("Speed Settings")]
[SerializeField] private float maxSpeed = 120f;
[SerializeField] private float acceleration = 25f;
// Declared at CLASS LEVEL so all methods can read and modify it
public float CurrentSpeed { get; private set; }
private void Update()
{
// Smoothly accelerate towards target max speed
CurrentSpeed = Mathf.MoveTowards(CurrentSpeed, maxSpeed, acceleration * Time.deltaTime);
// Translate aircraft along its forward flight vector
transform.Translate(Vector3.forward * (CurrentSpeed * Time.deltaTime));
}
}