error CS0131... driving me nutz :)

Here is a script for AI Motor Mapping, and I get all the time this error for CarContoller: “The left-hand side of an assignment must be a variable, a property or an indexer”

Tried to solve this, but in vein.

using UnityEngine;
using System.Collections;

public class AIMotorMapping : MonoBehaviour {
    [HideInInspector]
    public float steerInput;
    [HideInInspector]
    public float throttleInput;
    [HideInInspector]
    public float brakeInput;
    [HideInInspector]
    public float handbrakeInput;
    [HideInInspector]
    public float steerMax;
    [HideInInspector]
    public float speedMax;

    public Transform flWheelMesh;
    public Transform frWheelMesh;   
    public bool usingAIDriverMotor = true;

    private AIDriverMotor aIDriverMotor;
		
    void Awake()
    {
        if (usingAIDriverMotor)
        {
            aIDriverMotor = this.GetComponent<AIDriverMotor>();
            steerMax = aIDriverMotor.maxSteerAngle;
            speedMax = aIDriverMotor.maxSpeed;
        }
        else
        {
	    CarController = this.GetComponent<CarController>;
	    steerMax = 42;
	    speedMax = 250;
        }
    }

	void Update () {

        if (usingAIDriverMotor)
        {            
            aIDriverMotor.aiSteerAngle = steerInput;
            aIDriverMotor.aiSpeedPedal = throttleInput;
            aIDriverMotor.aiBrakePedal = brakeInput;
        }
        else
        {
			CarController.steerInput = steerInput;
            CarController.brakeInput = brakeInput;            
            if (throttleInput > 0)
            {
                CarController.throttleInput = throttleInput;
            }
            else
            {
                CarController.throttleInput = (-1) * throttleInput;
            }

        }
	}
}

There’s multiple issues…

CarController = this.GetComponent<CarController>;

is wrong for 2 issues. 1, you’re missing the name of the variable, you just have the type. 2, you’re missing the ‘()’ parenthesis’ after the GetComponent call.

Correct should be:

CarController myCarController = this.GetComponent<CarController>();

HOWEVER, you now locked into a bad scope. If you’re not sure what scope means, you need to bone up on your programming skills. Ultimately, you need to move the CarController variable to the top of your script, making it of Class-level scope (or a class variable) and update your calls like CarController.throttleInput to not use the Type, but rather the instance you’ve created.

… here is an abridged version:

using UnityEngine;
using System.Collections;
 
public class AIMotorMapping : MonoBehaviour {
    // ...omitted...

    public CarController myCarController; // new!!!
 
   // ...omitted...
 
    void Awake()
    {
        myCarController = GetComponent<CarController>(); //MOVED!!! and FIXED!!!

        //...omitted...
    }
 
    void Update () {
 
        if (usingAIDriverMotor)
        {            
           // ... omitted..
        }
        else
        {
         myCarController.steerInput = steerInput;
            myCarController.brakeInput = brakeInput;            
            if (throttleInput > 0)
            {
                myCarController.throttleInput = throttleInput;
            }
            else
            {
                myCarController.throttleInput = (-1) * throttleInput;
            }
 
        }
    }
}

This should work. I advise you to maybe putting game development on hold a bit until the programming skills catch up :slight_smile: you’ll have a much more enjoyable experience, I guarantee it.