Answer the question
In order to leave comments, you need to log in
Problem with PlayerPrefs?
Hello, I was saving the inventory and I encountered such a problem that when an item is thrown away, for some reason, the save goes through and the item, after being thrown out, goes to the next free slot.
Here is the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class sobiraenpredmetu : MonoBehaviour
{
private inventoryipob inventoey;
public GameObject slotButton;
public int id;
private void Start()
{
inventoey = GameObject.FindGameObjectWithTag("Player").GetComponent<inventoryipob>();
if (PlayerPrefs.HasKey("slots"))
{
for (int i = 0; i < inventoey.slots.Length; i++)
{
if (PlayerPrefs.GetInt($"сохраняем слот{i}") == i)
{
if (inventoey.idfull[i] == false)
{
if (id == PlayerPrefs.GetInt($"slots{id}"))
{
Instantiate(slotButton, inventoey.slots[i].transform);
inventoey.idfull[i] = true;
break;
}
}
}
}
}
}
private void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("Player"))
{
for (int i = 0; i < inventoey.slots.Length; i++)
{
if (inventoey.idfull[i] == false)
{
inventoey.idfull[i] = true;
Instantiate(slotButton, inventoey.slots[i].transform);
Destroy(gameObject);
PlayerPrefs.SetInt($"slots{id}", id);
PlayerPrefs.SetInt($"сохраняем слот{i}", i);
Debug.Log(PlayerPrefs.GetInt($"slots{id}", id) + " = id");
Debug.Log(PlayerPrefs.GetInt($"сохраняем слот{i}", i) + " = i");
break;
}
}
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class vubros : MonoBehaviour
{
public GameObject item;
private Transform player;
private void Start()
{
player = GameObject.FindGameObjectWithTag("Player").transform;
}
public void spawnDropedItem()
{
Vector3 playerPos = new Vector3(player.position.x + 3, player.position.y + 1,9);
Instantiate(item, playerPos, Quaternion.identity);
}
}
using System.Collections.Generic;
using UnityEngine;
public class slots : MonoBehaviour
{
private inventoryipob inventoey;
public int i;
private void Start()
{
inventoey = GameObject.FindGameObjectWithTag("Player").GetComponent<inventoryipob>();
}
private void Update()
{
if(transform.childCount <= 0)
{
inventoey.idfull[i] = false;
}
}
public void DropItem()
{
foreach(Transform child in transform)
{
child.GetComponent<vubros>().spawnDropedItem();
GameObject.Destroy(child.gameObject);
}
}
}
using System.Collections;
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question