0

I implemented a button that can switch between scene 1 and scene 2 on my software, but when I click on scene 2 and then go back to scene 1 the previous changes resets on scene 1, how can I keep these changes?

This is the script for home scene button:

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

public class HomeScript : MonoBehaviour
{
    public string sceneName; public Button loadSceneBtn;
    
    void Start()
    {
        loadSceneBtn.onClick.AddListener(ChangeScene);
    }

    void ChangeScene()
    {
        SceneManager.LoadSceneAsync("Home");
    }
}
Palle Due
  • 5,929
  • 4
  • 17
  • 32
  • `DontDestroyOnLoad` or `ScriptableObject` [DontDestroyOnLoad](https://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html) [ScriptableObject](https://docs.unity3d.com/Manual/class-ScriptableObject.html) – Panda Strong Jul 07 '20 at 11:02
  • can you explain better, cause i'm noob – Michele Loizzo Jul 07 '20 at 11:06
  • So you do it like this: 1. Create a new Tag on the Inspector called "DONTDESTROY". 2. Add this tag to all gameObjects you want to save your changes during scene change. 3. Add this script on start on your 2.Scene to mark all the objects you want to save : var allPersistanceObjects = GameObject.FindGameObjectsWithTag("DONTDESTROY"); foreach(var obj in allPersistanceObjects) { DontDestroyOnLoad(obj); } – VisDesign Jul 07 '20 at 13:50
  • @el6976 how can i add script on start of the scene? – Michele Loizzo Jul 08 '20 at 12:51
  • Just some script attached to a gameObject which is active when you load the scene. In the Start() method you only want to assign it once. – VisDesign Jul 08 '20 at 12:53
  • @el6976 im a noob, i create the tag dont destroy on my gameobject and select the tag, then i attached the script in the start method in the script on my post above, is it right? – Michele Loizzo Jul 08 '20 at 12:56
  • Creation of tag is indipendent of objects. First create the tag. 2. Assign it to your prefabs or gameobjects you want to save through scene switch. – VisDesign Jul 08 '20 at 12:58
  • Yest Start Method you have would work if this script is present in the scenene. Meaning is attached to one active gameobject – VisDesign Jul 08 '20 at 12:59
  • @el6976 wait i explain to you better, i have this program with 2 scene, in the first scene i have a bowel 3d model that can be dragged, rotated, painted and the camera can be zoomed with scrollwheel, ok now when i click on button scene 2 and then in this scene i click the button to return on scene 1 all my previuous changes do on my 3d bowel resetting, how can i keep? – Michele Loizzo Jul 08 '20 at 13:02
  • Than it is much simpler in the scene with changed bowl add somewhere DontDestroyOnLoad(bowl); and it should work just make sure the DontDestroyOnLoad is called. Maybe at the script in which you paint the bowl. You do have there a reference of it – VisDesign Jul 08 '20 at 13:06
  • @el6976ok so i will go to the paint script and what i have to insert? DontDestroyOnLoad(???) – Michele Loizzo Jul 08 '20 at 13:11
  • Yes and ??? Is your bowl reference. It could be a public variable where you assign the bowl gameobject or you could put. GameObject.Find("BOWL_NAME"). You should see more tutorials in my opinion – VisDesign Jul 08 '20 at 13:16
  • @el6976 yes i should, i tried and it doesnt work, the object still resetting position ecc – Michele Loizzo Jul 08 '20 at 13:21
  • Did you do it after the paint is applied or at the Start() – VisDesign Jul 08 '20 at 13:22
  • Write me an email at ervislilaj@hotmail.com i could help you on remote if you want. I use Anydesk for remote. – VisDesign Jul 08 '20 at 13:26
  • i insert the dont destroy at the start of my texturepainter script – Michele Loizzo Jul 08 '20 at 13:26
  • Ok try to add it at the end of painting. Some method you have there done painting. – VisDesign Jul 08 '20 at 13:29

0 Answers0