I need a help with a script i'm working on

When I try to run the script I get an error like this:
Assets \ Scripts \ CarsGenerator.cs (60,26): error CS0117: ‘MapGenerator’ does not contain a definition for ‘instance’

In my script in line 60 I tried to write that the speed of my MapGenerator, (which is another script) will not be equal to 0, but when I try to run the project I get an error:
Assets \ Scripts \ CarsGenerator.cs (60,26): error CS0117: ‘MapGenerator’ does not contain a definition for ‘instance’

I would be very happy if you help me.

The Script:

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

public class CarsGenerator : MonoBehaviour
{
    int itemSpace = 15;
    int itemCountInMap = 7;
    public float laneOffset = 2.5f;
    int coinsCountInItem = 10;
    float coinsHeight = 0.5f;
    int carSize;

    enum TrackPos { Left = -1, Center = 0, Right = 1};
    enum CoinsStyle { Line };

    public GameObject BusBluePrefab;
    public GameObject RubbishTruckPrefab;
    public GameObject TaxiPrefab;
    public GameObject VanYellowPrefab;
    public GameObject CarBluePrefab;
    public GameObject IceCreamTruckPrefab;
    public GameObject CoinPrefab;

    public List<GameObject> cars = new List<GameObject>();
    public List<GameObject> activeCars = new List<GameObject>();

    static public CarsGenerator instance;

    struct MapItem
    {
        public void SetValues(GameObject obstacle, TrackPos trackPos, CoinsStyle coinsStyle)
        {
            this.obstacle = obstacle; this.trackPos = trackPos; this.coinsStyle = coinsStyle;
        }
        public GameObject obstacle;
        public TrackPos trackPos;
        public CoinsStyle coinsStyle;
    }

    private void Awake()
    {
        instance = this;
    }

    void Start()
    {
        carSize = itemCountInMap * itemSpace;
        cars.Add(MakeCar1());
        cars.Add(MakeCar1());
        cars.Add(MakeCar1());
        foreach (GameObject car in cars)
        {
            car.SetActive(false);
        }
    }

    void Update()
    {
        if (MapGenerator.instance.speed == 0) return;

        foreach (GameObject car in activeCars)
        {
            car.transform.position -= new Vector3(0, 0, MapGenerator.instance.speed * Time.deltaTime);
        }
        if (activeCars[0].transform.position.z < -carSize)
        {
            RemoveFirstActiveCar();
            AddActiveCar();

        }
    }

    void RemoveFirstActiveCar()   
    {
        activeCars[0].SetActive(false);
        cars.Add(activeCars[0]);
        activeCars.RemoveAt(0);
    }

    public void ResetCars()
    {
        while (activeCars.Count > 0)
        {
            RemoveFirstActiveCar();
        }
        AddActiveCar();
        AddActiveCar();
    }

    void AddActiveCar()
    {
        int r = Random.Range(0, cars.Count);
        GameObject go = cars[r];
        go.SetActive(true);
        foreach (Transform child in go.transform)
        {
            child.gameObject.SetActive(true);
        }
        go.transform.position = activeCars.Count > 0 ?
                                activeCars[activeCars.Count - 1].transform.position + Vector3.forward * carSize :
                                new Vector3(0, 0, 10);
        cars.RemoveAt(r);
        activeCars.Add(go);
    }

    GameObject MakeCar1()
    {
        GameObject result = new GameObject("Car1");
        result.transform.SetParent(transform);
        for (int i = 0; i < itemCountInMap; i++)
        {
            GameObject obstacle = null;
            TrackPos trackPos = TrackPos.Center;
            CoinsStyle coinsStyle = CoinsStyle.Line;

            if(i ==2) { trackPos = TrackPos.Left; obstacle = BusBluePrefab; }
            else if(i == 3) { trackPos = TrackPos.Left; obstacle = TaxiPrefab; }
            else if(i == 3) { trackPos = TrackPos.Left; obstacle = TaxiPrefab; }

            Vector3 obstaclePos = new Vector3((int)trackPos * laneOffset, 0, i * itemSpace);
            CreateCoins(coinsStyle, obstaclePos, result);
            if (obstacle != null)
            {
                GameObject go = Instantiate(obstacle, obstaclePos, Quaternion.identity);
                go.transform.SetParent(result.transform);
            }
        }
        return result;
    }
    void CreateCoins(CoinsStyle style, Vector3 pos, GameObject parentObject)
    {
        Vector3 coinPos = Vector3.zero;
        if (style == CoinsStyle.Line)
        {
            for (int i = -coinsCountInItem/2; i < coinsCountInItem / 2; i++)
            {
                coinPos.y = coinsHeight;
                coinPos.z = i * ((float)itemSpace / coinsCountInItem);
                GameObject go = Instantiate(CoinPrefab, coinPos + pos, Quaternion.identity);
                go.transform.SetParent(parentObject.transform);
            }
        }
    }
}

and the another script (MapGenerator):

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

public class MapGenerator : MonoBehaviour
{
    public GameObject MapPrefab;
    private List<GameObject> maps = new List<GameObject>();
    public float maxSpeed = 10;
    private float speed = 0;
    public int maxMapCount = 5;

    void Start()
    {
        ResetLevel();
        //StartLevel();
    }

    // Update is called once per frame
    void Update()
    {
        if (speed == 0) return;
        foreach (GameObject map in maps)
        {
            map.transform.position -= new Vector3(0, 0, speed * Time.deltaTime);
        }

        if(maps[0].transform.position.z < -40)
        {
            Destroy(maps[0]);
            maps.RemoveAt(0);

            CreateNextMap();
        }
    }

    private void CreateNextMap()
    {
        Vector3 pos = Vector3.zero;
        if (maps.Count > 0) { pos = maps[maps.Count - 1].transform.position + new Vector3(0, 0, 40); }
        GameObject go = Instantiate(MapPrefab, pos, Quaternion.identity);
        go.transform.SetParent(transform);
        maps.Add(go);
    }

    public void StartLevel()
    {
        speed = maxSpeed;
        SwipeManager.instance.enabled = true;
    }
   

    public void ResetLevel()
    {
        speed = 0;
        while(maps.Count > 0)
        {
            Destroy(maps[0]);
            maps.RemoveAt(0);
        }
        for(int i = 0; i < maxMapCount; i++)
        {
            CreateNextMap();
        }
        SwipeManager.instance.enabled = false;
        CarsGenerator.instance.ResetCars();
    }
}

The error tells you exactly what is wrong.

Can you see “instance” defined anywhere in it? No, there isn’t one.

You defined “CarsGenerator.instance” so presumably you wanted the same in this class?

Given your reporting this, maybe you’re copying this code from somewhere and are just confused on what it all means?

2 Likes