Home Game Development unity – Passing objects through scenes

unity – Passing objects through scenes

0
unity – Passing objects through scenes

[ad_1]

I thought this would have been asked already but I could not find an answer to it so sorry if it has been asked already.

I am making a VR game in Unity using the Hurricane VR Framework. Now I am trying to create an inventory system like Saints & Sinners, with a backpack, but I could not make it work.

I was able to make it so that you can grab items and store them in the backpack, it is very easy with this framework, the problem is passing those objects to the next scene where I get this error:

The object of type ‘GameObject’ has been destroyed but you are still
trying to access it.

What I have now is this game manager:

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

public class GameManager : MonoBehaviour
{

    public static GameManager Instance;
    // List to store the object and the tag of the socket where it was stored
    private static List<(string, GameObject)> Inventory = new List<(string, GameObject)>();
    // Array with the tags of the inventory sockets.
    private string[] tags = { "I11", "I12", "I13", "I21", "I22", "I23", "HolsDer", "HolsIz"}; 

    private void Awake()
    {

        if(Instance==null)
        {
            Instance = this;
            DontDestroyOnLoad(gameObject);
        }
        else
        {
            if(Instance!=null)
            {
                Destroy(gameObject);
            }
        }
    }
    // Add a new tuple to the inventory
    public void addObject(string socket, GameObject objeto)
    {
        if (objeto != null)
        {
            Debug.Log(objeto.tag);
            Inventory.Add((socket, objeto));
        }
        else
        {
            Debug.Log("Object is null");
        }
    }
    
    public void nextScene(int scene)
    {
        // Iterate through the tags of the sockets to find objects in the current scene
        foreach (string tag in tags)
        {
            // Search the sockets by tag
            GameObject[] sockets = GameObject.FindGameObjectsWithTag(tag);
            if (sockets.Length > 0)
            {
                GameObject socket = sockets[0];

                // Check if the socket has a child (It always has one child so the condition 
                // is that it must have 2, the default and the object)
                if (socket.transform.childCount == 2)
                {
                    GameObject objeto = socket.transform.GetChild(1).gameObject;
                    DontDestroyOnLoad(objeto);

                    // Add the object to the Inventory list
                    addObject(tag, objeto);
                }
                else
                {
                    Debug.Log("No childs in socket "+ tag);
                }
            }
            
        }

        // Load next scene
        SceneManager.LoadScene(scene);
    }
    // This is called at the start of each scene.
    public void restoreObjects()
    {
        foreach ((string socketName, GameObject objeto) in Inventory)
        {
            // Find the socket where the object should be placed
            GameObject[] sockets = GameObject.FindGameObjectsWithTag(socketName);

            if (sockets.Length > 0)
            {
                GameObject socket = sockets[0];
                // Check for null objects or sockets (This is where it fails, the objects are all null)
                if(objeto == null)
                {
                    Debug.Log("objeto is null");
                }
                if (socket == null)
                {
                    Debug.Log("Socket is null");

                }
                // This fails throwing this error: The object of type 'GameObject' has been destroyed but you are still trying to access it.
                GameObject newObject = Instantiate(objeto, socket.transform);
            }
            else
            {
                Debug.LogWarning("Socket with tag '" + socketName + "' not found.");
            }
        }
    }
}

The nextScene() function is called when pressing a button, just for testing and the restoreObjects() one is called at the start of the next scene and that is when it fails because the object is null.

What am I doing wrong?

[ad_2]

Previous article The most disappointing laptops of 2023
Next article Eve Online player corp names ChatGPT its new leader
Hello there! My name is YoleeTeam, and I am thrilled to welcome you to AmazonianGames.com. As the premier destination for all things related to Amazon Games' universe, we are dedicated to providing you with the most exciting and immersive gaming experiences out there. From captivating visuals to exhilarating gameplay, our website is packed with comprehensive insights, updates, and reviews to keep you ahead of the game. Whether you're a seasoned gamer or new to the scene, I am here to guide you through this virtual frontier. If you have any questions or suggestions, feel free to reach out to me at john@yoleesolutions.com. Embark on your Amazon gaming journey today with AmazonianGames.com and let the adventure begin!