My script lose reference of another object when load

Discussion and help for Easy Save 3
Post Reply
socker
Posts: 3
Joined: Thu Mar 17, 2022 11:12 pm

My script lose reference of another object when load

Post by socker »

Hi, im using easy save for the first time and im in love,
I have a proceduraly game like the binding of isaac, for go to other room i create it some points that have the reference of the next room,
my problem come when i load all the scene, some references to the other room works good and others have a null reference.

I dont know how to solve this, im autosaving the scene with events in none and saving with ES3.AUTOSAVEMSG.SAVE()

How i handle the save : There is a gameobject that have all the rooms as a child and i just save that gameobject with Save children enabled.

Thanks for your time, and sorry for bother :( i tried xD

Image https://imgur.com/a/JpMsKnx

Image https://imgur.com/usEzJJS
User avatar
Joel
Moodkie Staff
Posts: 4849
Joined: Wed Nov 07, 2012 10:32 pm

Re: My script lose reference of another object when load

Post by Joel »

Hi there, and thanks for getting in contact.

The reference you’re trying to save: is it generated at runtime, or does the object exist in your scene prior to entering play mode? Are you getting any warnings in the console?

Please could you also post the script itself so I can try to replicate it at my end.

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
socker
Posts: 3
Joined: Thu Mar 17, 2022 11:12 pm

Re: My script lose reference of another object when load

Post by socker »

Hi, thanks for reply so fast :3

It is generated in runtime, i have rooms prefabs with doors like "north south east west" with this script , it have a collider in each one , when the collider detect some other door my script get the reference of the next point and destroys the collider.
My rooms are generated like choose a direction nort south east or west and just apply +30 or -30 in the direction.
All works good in the first cycle all the reference doors works good , then i save my gameobject Parent that have all rooms as a child and i put my script to ES3types so they are saved.
When i load as i say previously some "nextDoors" are null , no errors. no nothing.


You can see in the scripts that some rooms are disabled, but this dosnt affect , some disabled rooms have "nextDoor" good and some others null.


My doors are created like that https://imgur.com/a/zEhUqOp

Big collider is for check next door and small collider inside Door child, just for collide with the player, the reference spawn is just a gameobject that you can ignore it ,is just for check when i need to go to teleport(it have no real doors xD) . Hope it helps

Thanks for helping.




Code: Select all

public class NewDoorTeleport : MonoBehaviour
{
    
    public GameObject doorSpawn;


    public Transform cameraPosition;
    
    public GameObject nextDoor;


    
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        if (nextDoor != null)
        {
            Destroy(this.GetComponent<BoxCollider>());
        }
    }

    private void OnTriggerStay(Collider other)
    {
        
   
        
        if (other.CompareTag("Exits"))
        {
            
            other.GetComponent<NewDoorTeleport>().nextDoor = doorSpawn;
            nextDoor = other.GetComponent<NewDoorTeleport>().doorSpawn;
            Destroy(transform.gameObject.GetComponent<BoxCollider>());
            
      //     
          
          
        }
        
        if (other.gameObject.tag.Equals("Player"))
        {
          
            
            //    EnableNextRoom();
            
            
         
            // DisableThisRoom();

            EnableAndDisableRoom(other);

        }
        
    }
    
    
    public void EnableAndDisableRoom( Collider other)
    {
       
        EnableNextRoom();
   //     Camera.main.transform.position = nextDoor.transform.parent.GetComponent<DoorTeleport>().cameraPosition.position;
   //     Camera.main.transform.eulerAngles = new Vector3(50, 0, 0);  //nextDoor.transform.parent.GetComponent<DoorTeleport>().cameraPosition.rotation;
   //     Camera.main.GetComponent<CameraFollow>().enabled =false;
     //   other.GetComponent<CharacterController>().enabled = false;
        
     //   other.GetComponent<CharacterController>().enabled = false;
        other.transform.position = nextDoor.transform.position;
    //    yield return new WaitForSeconds(0.1f);
        DisableThisRoom();
        
    }
    
    public void EnableNextRoom()
    {
        
      //  doorSpawn.transform.parent.GetComponent<DoorTeleport>().transform.parent.gameObject.SetActive(false);

      var t = nextDoor.transform.parent.GetComponent<NewDoorTeleport>().transform.parent.gameObject
          .GetComponentsInChildren(typeof(Transform), true);
              
                for (int j = 0; j < t.Length; j++)
                {
                  
                        
                        if (t[j].gameObject.tag.Equals("Enemy"))
                        {
                            
                            t[j].gameObject.GetComponent<EasyStateMachine>().RecycleMachine();
                            
                        }
                        
                        t[j].GameObject().SetActive(true);
                  
                }
                // finalRooms[i].GameObject().SetActive(false);
                
            
        
    } 
    
    public void DisableThisRoom()
    {
        
      //  doorSpawn.transform.parent.GetComponent<DoorTeleport>().transform.parent.gameObject.SetActive(false);

      bool isTeleportRoom = false;
                var t = doorSpawn.transform.parent.GetComponent<NewDoorTeleport>().transform.parent.gameObject.GetComponentsInChildren<Transform>();
                for (int j = 1; j < t.Length; j++)
                {
                    if (t[j].gameObject.name.Contains("Minimap"))
                    {
                        t[j].GameObject().SetActive(true);
                    }
                    else
                    {
                        if (t[j].gameObject.name.Contains("Teleport"))
                        {
                            isTeleportRoom = true;
                            break;
                        }
                        t[j].GameObject().SetActive(false);
                    }
                  
                }

                if (isTeleportRoom)
                {
                    for (int j = 1; j < t.Length; j++)
                    {
                        
                            t[j].GameObject().SetActive(true);
                        
                        
                  
                    }
                }
                // finalRooms[i].GameObject().SetActive(false);
                
            
        
    }
}
Last edited by socker on Fri Mar 18, 2022 12:22 pm, edited 1 time in total.
User avatar
Joel
Moodkie Staff
Posts: 4849
Joined: Wed Nov 07, 2012 10:32 pm

Re: My script lose reference of another object when load

Post by Joel »

Hi there,

As there are no warnings, it does not sound like it's because it can't find a reference. In this case it sounds like something is overwriting the variable.

Please could you create a new project with a very simple scene which replicates your issue and private message it to me with step-by-step instructions so I can see what is happening.

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
socker
Posts: 3
Joined: Thu Mar 17, 2022 11:12 pm

Re: My script lose reference of another object when load

Post by socker »

Pmed
BoBlond
Posts: 11
Joined: Thu Jul 29, 2021 10:54 am

Re: My script lose reference of another object when load

Post by BoBlond »

I have a similar problem, did you find a solution?
User avatar
Joel
Moodkie Staff
Posts: 4849
Joined: Wed Nov 07, 2012 10:32 pm

Re: My script lose reference of another object when load

Post by Joel »

BoBlond wrote: Thu Jul 27, 2023 8:46 pm I have a similar problem, did you find a solution?
Hi there,

As this is quite an old thread, please could you make a new thread describing your issue with information about your project. The behaviour mentioned in this post can happen for quite a number of reasons so I would need more specific information to understand why it's happening in your project.

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
BoBlond
Posts: 11
Joined: Thu Jul 29, 2021 10:54 am

Re: My script lose reference of another object when load

Post by BoBlond »

Joel wrote: Fri Jul 28, 2023 8:11 am
BoBlond wrote: Thu Jul 27, 2023 8:46 pm I have a similar problem, did you find a solution?
Hi there,

As this is quite an old thread, please could you make a new thread describing your issue with information about your project. The behaviour mentioned in this post can happen for quite a number of reasons so I would need more specific information to understand why it's happening in your project.

All the best,
Joel
Thank you, I just did here :)

https://moodkie.com/forum/viewtopic.php?t=2875
Post Reply