UI Position save in PlayerPrefs

Hi! I have a little big problem… In my game, the player goes forward, and at some point the player hits an invisible cube that activates in the shop a new object. That object is covered by an image with a lock, which disappears when the player touches that cube. The image doesn’t disappear completely, it just moves its position very high, where you can’t see me in the frame. The problem is that I want the position of this image to be saved in PlayerPrefs… I’ve tried many solutions found, but it doesn’t work perfectly. The problem is, what I found, when I press “load” the image does not go to the position that I told him, or when I enter the game, the image goes anywhere on the screen, without having done anything before …
This is the script that activates a new object in the shop:

 public GameObject Color1;
    public GameObject Color1Cube;

    public GameObject Color2;
    public GameObject Color2Cube;

    public GameObject Color3;
    public GameObject Color3Cube;


    void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.tag == "1color")
        {
            Color1.transform.localPosition = new Vector3(0, 1500, 0);
            Color1Cube.SetActive(false);
        }

        if (collision.gameObject.tag == "2color")
        {
            Color2.transform.localPosition = new Vector3(0, 1500, 0);
            Color2Cube.SetActive(false);
        }

        if (collision.gameObject.tag == "3color")
        {
            Color3.transform.localPosition = new Vector3(0, 1500, 0);
            Color3Cube.SetActive(false);
        }
    }

I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run?
  • what are the values of the variables involved? Are they initialized?

Knowing this information will help you reason about the behavior you are seeing.

You say you’re having a problem with PlayerPrefs, but I don’t see any references to PlayerPrefs in your posted code.

1 Like