Newly Selected Value Saved Correctly But Not Loaded

Hello there,

In my car game, I assign to the car according to the selected cover and save it. I’m having a problem with my test scenario;
1-) The player selects a cover in the garage with his tool and changes it.
2-) In the same scene, he returns to the main menu and selects another vehicle.
3-) When the second vehicle of its choice wants to change the cover, THE COVER of THE FIRST VEHICLE CHANGES ! No change(no new load cover) for the second vehicle.

All operations are done in one scene and I try to manage it with two scripts;
GeneralGameManager.cs - On the MainMenu
LoadCover.cs - Attached to each cars

This debug updates correctly when I select other car for _currentCoverID and GCurrentCarID;
Debug.Log(“Load Cover:”+ _currentCoverID+GeneralGameManager.instance.GCurrentCarID);

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

public class GeneralGameManager : MonoBehaviour
{
    #region Singleton
    private static GeneralGameManager _Instance;
    public static GeneralGameManager instance
    {
        get
        {
            if (_Instance == null)
                _Instance = GameObject.FindObjectOfType<GeneralGameManager>();
            return _Instance;
        }
    }
    #endregion

    public Texture[] CarCover;
    public int currentCoverID;
    public int GCurrentCarID;

    void Start()
    {
        DontDestroyOnLoad(gameObject);
    }

    public void CoverSelect(int _cov)
    {
        if (GCurrentCarID == 0 || GCurrentCarID == 1 || GCurrentCarID == 2 || GCurrentCarID == 3)
        {
            Debug.Log("GCurrentID: " + GCurrentCarID.ToString());
            currentCoverID = _cov;
            LoadCover.instance.m_Renderer.sharedMaterial.SetTexture("_MainTex", CarCover[currentCoverID]);
            PlayerPrefs.SetInt("currentCoverID" + GCurrentCarID, currentCoverID);
            LoadCover.instance.LoadtheCover();
        }
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LoadCover : MonoBehaviour
{
    #region Singleton
    private static LoadCover _Instance;
    public static LoadCover instance
    {
        get
        {
            if (_Instance == null)
                _Instance = GameObject.FindObjectOfType<LoadCover>();
            return _Instance;
        }
    }
    #endregion

    public Renderer m_Renderer;
    public int _currentCoverID;
    //public int _closeoverlap;


    // Start is called before the first frame update
    public void Start()
    {
        m_Renderer = GetComponent<Renderer>();
        StartCoroutine(CoverStarting());
    }

    IEnumerator CoverStarting()
    {
        yield return new WaitForEndOfFrame();
        LoadtheCover();
    }

    public void LoadtheCover()
    {

        if (GeneralGameManager.instance.GCurrentCarID == 0 || GeneralGameManager.instance.GCurrentCarID == 1 || GeneralGameManager.instance.GCurrentCarID == 2 || GeneralGameManager.instance.GCurrentCarID == 3)
        {
            m_Renderer = GetComponent<Renderer>();
            m_Renderer = GetComponent<Renderer>();
            _currentCoverID = PlayerPrefs.GetInt("currentCoverID"+"||" + GeneralGameManager.instance.GCurrentCarID, 1511);
            m_Renderer.sharedMaterial.SetTexture("_MainTex", GeneralGameManager.instance.CarCover[_currentCoverID]);

            Debug.Log("Load Cover:"+ _currentCoverID+"||"+GeneralGameManager.instance.GCurrentCarID);
        }

    }
    }

You are saving to the key
"currentCoverID" + GCurrentCarID
but loading from the key
"currentCoverID"+"||" + GCurrentCarID

There might be more wrong than that, but that is definitely enough to break your save/load system.

Yes you are right, But this is only in Debug.Log and I continue to have my problem even though I fixed it.

It was NOT only in Debug.Log. Compare line 37 of the first file to line 46 of the second file.

1 Like

Again you are right, but the problem still exists even though I fixed it in two areas

I don’t see any other problems that are so obvious that I can diagnose them without you giving a clearer statement of your test results. There are several key values in your code that hypothetically could be wrong:

  • GeneralGameManager.GCurrentCarID
  • GeneralGameManager.currentCoverID
  • LoadCover.m_Renderer
  • LoadCover._currentCoverID
  • GeneralGameManager.CarCover[_currentCoverID]

You are already logging some of those, but not all of them–and for the ones you are logging, you haven’t actually told me which specific ones were correct or incorrect at what points during your test.

I uploaded a video to elaborate and to be understood more easily;

During this test;
first, GCurrentCarID = 0 and cover selection successful
then I choose GCurrentCarID = 1 and debug also appears and
then I selected 7 as cover. For car number 1 and cover=7 is saved (look at debuging) but not loaded(not shown) on cover7 for GCurrentCarID = 1
finnaly When I go to car selection screen, select GCurrentCarID = 0, I was shown Car0’s cover is 7!

my scripts screen shots and I add next car selection function(In MaainMenu.cs);

GeneralGameManager.cs;

LoadCover.cs;

MaainMenu.cs;

Your debug log says that cover 7 was loaded (I don’t see any log messages for saving). Looks to me like probably you are loading the correct data from PlayerPrefs, but then applying the result to the wrong visual object in your scene.

Your LoadCover class just uses GetComponent() to decide which renderer to modify, regardless of which car is selected. That might be fine if you are using the same Renderer to represent different cars depending on which one is “selected”, but it looked like you have separate car objects that are being set active or inactive, in which case modifying the same Renderer for every single car is probably not what you want.

Antistone, Your determination seems very correct,
Thank you very much.
I will be doing research for the solution.

I provided a solution by handling the renderers separately.
Thanks again,Antistone