Need help understanding object/target jargon for dontdestroyonload

Hi, I’m trying to save the player’s chosen character name between scenes. I have a text field for them on one scene’s GUI, as a public string on the GUI, and I have tried attaching this information to a cube that should not be destroyed on the next load, using dontdestroyonload, but I get errors.

I don’t understand Unity’s documentation of dontdestroyonload… I’ve tried many things including attaching the script to different objects, scripts or scenes, but my problem definitely lies in understanding the jargon.

Here are the two examples Unity gives:
public static void DontDestroyOnLoad(Object target);
DontDestroyOnLoad(transform.gameObject);

What do Object, target, transform.gameObject mean? It is not intuitive to me. I have tried assigning my cube as gameObject, targeting the cube or different scripts, etc.

Here is the way I have it set up the way I think it should be:
scene 1’s code defining public string charnamestring as an editable string, with a textfield to edit it on the gui
scene 1’s code saying don’t destroy cubetest (as just DontDestroyOnLoad(cubetest); for now, but I have also tried assigning more than just the gameObject)
cubetest’s code defining simply “public string charnamestring;”
scene 2 which I don’t even worry about because I get errors in assigning the dontdestroyonload.

I get three errors:

  1. The name `cubetest’ does not exist in the current context
  2. The best overloaded method match for `UnityEngine.Object.DontDestroyOnLoad(UnityEngine.Object)’ has some invalid arguments
  3. Argument #1' cannot convert object’ expression to type `UnityEngine.Object’

I probably posted this wrong, it’s my first time posting, but I would sincerely appreciate any insight that could be given. Thank you.

Object is a type, target is the object that you want DontDestroyOnLoad to operate on (which is of type Object), and transform.gameObject refers to the GameObject that the transform component is attached to. You can’t use strings to refer to objects; you can only use the object itself. You should post the code that you’re actually using (use code tags when posting code).

–Eric

Thank you for your help Eric! I apologize, but I am still not getting it.
Object would be cube (a type of object), target would be cubetest, the name of the specific cube with the attached script? DontDestroyOnLoad(cube.cubetest)?

//SCENE 1

using UnityEngine;
using System.Collections;

public class scene1mainscript : MonoBehaviour {

        public string charnamestring = "Character Name";

        void Awake() {
        DontDestroyOnLoad(cubetest);
        }
      
    void OnGUI () {
      
        charnamestring = GUI.TextField(new Rect((Screen.width/2)-33, Screen.height-320, 200, 20), charnamestring, 25);
        }
    }

//CODE ATTATCHED TO CUBE THAT IS WITHIN SCENE 1
//The cube is named cubetest

using UnityEngine;
using System.Collections;

public class thescriptattatchedtocube : MonoBehaviour {

        public string charnamestring;
  
}

//SCENE 2

using UnityEngine;
using System.Collections;

public class scene2mainscript : MonoBehaviour {
      
            void OnGUI () {
        GUI.Label (new Rect((Screen.width/2)-33, Screen.height-280, 200, 20), charnamestring);
        }  
}

You need to hold a reference to your Cube within your main script.

public class scene1mainscript : MonoBehaviour {
        public GameObject CubeTest;
        public string CharNameString = "Character Name";

        private CubeNameScript cubeScript;
        void Awake() {
        DontDestroyOnLoad(CubeTest);
        cubeScript = CubeTest.GetComponent<CubeNameScript >();
        }

    void OnGUI () {

        CharNameString = GUI.TextField(new Rect((Screen.width/2)-33, Screen.height-320, 200, 20),  CharNameString , 25);
        cubeScript.CharNameString = CharNameString;
        }
    }

using UnityEngine;
using System.Collections;

public class scene2mainscript : MonoBehaviour{ 

public CubeNameScript cubeScript;

void Awake()
{  
 cubeScript = GameObject.Find("CubeTest").GetComponent<CharacterNameScript>();
}

   void OnGUI () {
     GUILabel(new Rect((Screen.width/2)-33, Screen.height-280, 200, 20), cubeScript.CharNameString);
        } 
}

In the Unity Editor you need to drag and drop the Cube GameObject in your scene onto the cubetest field in the Inspector.

You may want to look at some basics of coding if you neither understand the signature/example given in the documentation nor Erics post, unless it’s really just this example which causes confusion.

I could only repeat Erics post in other words, but i think it would be more helpful to look that up in detail.