0

I have written a script where i enable(active) the CarController script attach to the Car. I have taken car from UnityStandardAssets.

Initially CarController script component will be unactive as i dont want the car to move on certain occations.

Below is script to set the car in motion.

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

public class CarControlActive : MonoBehaviour
{
    public GameObject CarControl;
    
    void Start()
    {
        CarControl.GetComponent<UnityStandardAssets.Vehicles.Car.CarController>().enabled = true;
    }
}

I have attched the above script to an empty gameobject(CarControlActive).

But, when i set the CarController script component unactive i get the following error on my console.

enter image description here

Shubhro
  • 35
  • 5
  • did you reference `CarControl` via the Inspector and re you sure it has that compoennt attached? In general it would be better to directly make it `public UnityStandardAssets.Vehicles.Car.CarController CarControl;`. Then it is save that the referenced object definitely **has** such component and you can get rid of `GetComponent` and just use `CarControl.enabled = true;` – derHugo Feb 25 '22 at 09:21
  • yes it is attached to the Car (referenced by CarControl) and i have edited the scipt too but still getting this error – Shubhro Feb 25 '22 at 09:41
  • Then it is not referenced correctly ... have you checked if there is any "zomby" instance if this script at a wrong place maybe? Try to search for `t:CarControlActive` in your scene and check if there is any unexpected instance – derHugo Feb 25 '22 at 09:58
  • no results for t:CarControlActive, but CarControlActive on searching is there. – Shubhro Feb 25 '22 at 14:48
  • I solved it instead of CarController script, i can enable/disable CarUserControl script which is also attached in Car – Shubhro Feb 25 '22 at 15:05

0 Answers0