How to compare a string in a list with a gameobject.name from another list of gameobjects

Hello, I have created a shop system. In which the players can buy cars, this works completely fine. The cars are added to a scriptable object and are placed in the correct place. Element 0 = player 1.
Element 1 = player 2.
Element 2 = player 3.
etc etc.
Only if you quit the game now it will not save that list. This is because scriptable objects are not stored in build mode, but only in the Unity Editor.

So now I had an idea of how to fix this possibly. Since you cannot save a list of game objects. Namely this;

  1. save a list of strings. //for example List carNames; //Example in list is BMWCarNumber1
  2. get a list with every car in the game //BMWCarNumber1 but also MercedesCarNumber2 for example
  3. see which strings are equal to which game object //For example string BMWCarNumber1 == BMWCarNumber1.name
  4. Add BMWCarNumber1 to a list of all available cars & see if the car is already in the list.
    If so, we will not add it. If not, we will add it. //For Example in List AvailableCars is now BMWCarNumber1
  5. The player can now choose from any car in AvailableCars

only I have no idea how to compare a string in a list with a gameobject.name from another list of gameobjects

Hopefully someone can help me with this :slight_smile:

Class InventoryCar

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

[CreateAssetMenu(fileName = "Data", menuName = "Inventory/ListCar", order = 1)]
public class InventoryCar : ScriptableObject
{
    public List<ItemCar> inventoryItem = new List<ItemCar>();
    public List<GameObject> allCars;
    public List<string> carNames;
    private int savedListCount;
    public bool b_mobile = false;
    public float mobileMaxSpeedOffset = 0;
    public float mobileWheelStearingOffsetReactivity = 0;
    public bool b_Countdown = true;
    public bool b_LapCounter = true;

    public void Save()
    {
        for (int i = 0; i < carNames.Count; i++)
        {
            PlayerPrefs.SetString("CarNames" + i, carNames[i]);
        }
        PlayerPrefs.SetInt("CarNamesCount", carNames.Count);
    }

    public void Load()
    {
        carNames.Clear();
        savedListCount = PlayerPrefs.GetInt("CarNamesCount");

        for (int i = 0; i < savedListCount; i++)
        {
            string carName = PlayerPrefs.GetString("CarNames" + i);
            carNames.Add(carName);
        }
    }

    public void Check()
    {

    }
}

Class ItemCar

// Descrition : Use to create list of car that could be used by players in a race
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[System.Serializable]   
public class ItemCar{
    public List<GameObject> Cars = new List<GameObject>();                                                // List of cars for players or CPUs
}

you can do the following:

foreach (GameObject car in Cars)
{
    if (!carNames.Contains(car.name))
    {
        carNames.Add(car.name);
    }
}