VEHICLE SCRIPTING • INTER-SCRIPT COMMUNICATION

EngineController does not contain a definition for 'currentSpeed'

Occurs when an audio script or UI speedometer tries to read engineController.currentSpeed, but the variable is declared private or named differently.

1

Expose a Public Read-Only Property in EngineController.cs

Open your EngineController.cs (or CarController.cs) and add this public property:

EngineController.cs (C#)
using UnityEngine;

public class EngineController : MonoBehaviour
{
    private Rigidbody rb;

    private void Awake()
    {
        rb = GetComponent<Rigidbody>();
    }

    // ✅ EXPOSE THIS PROPERTY:
    // Calculates real-time speed in meters per second (m/s) or km/h
    public float currentSpeed => rb != null ? rb.linearVelocity.magnitude : 0f;

    public float CurrentSpeedKmh => currentSpeed * 3.6f;
}

Vehicle Architecture Tutorials

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.

AircraftSpeedController.cs (Proper Class Scope)
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));
    }
}

Public Properties vs Private Serialized Fields

In game code architectures, expose speed variables via read-only auto-properties like public float CurrentSpeed { get; private set; }. This allows UI speedometer scripts and sound pitch modulators to safely read the flight velocity without allowing outside systems to overwrite flight simulation state.