Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Need help understanding object/target jargon for dontdestroyonload

Discussion in 'Scripting' started by Linfen, Dec 6, 2014.

  1. Linfen

    Linfen

    Joined:
    Dec 6, 2014
    Posts:
    2
    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.
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    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
     
  3. Linfen

    Linfen

    Joined:
    Dec 6, 2014
    Posts:
    2
    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)?

    Code (CSharp):
    1. //SCENE 1
    2.  
    3. using UnityEngine;
    4. using System.Collections;
    5.  
    6. public class scene1mainscript : MonoBehaviour {
    7.  
    8.         public string charnamestring = "Character Name";
    9.  
    10.         void Awake() {
    11.         DontDestroyOnLoad(cubetest);
    12.         }
    13.      
    14.     void OnGUI () {
    15.      
    16.         charnamestring = GUI.TextField(new Rect((Screen.width/2)-33, Screen.height-320, 200, 20), charnamestring, 25);
    17.         }
    18.     }
    19.  
    20. //CODE ATTATCHED TO CUBE THAT IS WITHIN SCENE 1
    21. //The cube is named cubetest
    22.  
    23. using UnityEngine;
    24. using System.Collections;
    25.  
    26. public class thescriptattatchedtocube : MonoBehaviour {
    27.  
    28.         public string charnamestring;
    29.  
    30. }
    31.  
    32. //SCENE 2
    33.  
    34. using UnityEngine;
    35. using System.Collections;
    36.  
    37. public class scene2mainscript : MonoBehaviour {
    38.      
    39.             void OnGUI () {
    40.         GUI.Label (new Rect((Screen.width/2)-33, Screen.height-280, 200, 20), charnamestring);
    41.         }  
    42. }
    43.  
     
  4. LiberLogic969

    LiberLogic969

    Joined:
    Jun 29, 2014
    Posts:
    138
    You need to hold a reference to your Cube within your main script.

    Code (CSharp):
    1. public class scene1mainscript : MonoBehaviour {
    2.         public GameObject CubeTest;
    3.         public string CharNameString = "Character Name";
    4.  
    5.         private CubeNameScript cubeScript;
    6.         void Awake() {
    7.         DontDestroyOnLoad(CubeTest);
    8.         cubeScript = CubeTest.GetComponent<CubeNameScript >();
    9.         }
    10.  
    11.     void OnGUI () {
    12.  
    13.         CharNameString = GUI.TextField(new Rect((Screen.width/2)-33, Screen.height-320, 200, 20),  CharNameString , 25);
    14.         cubeScript.CharNameString = CharNameString;
    15.         }
    16.     }
    17.  
    18. using UnityEngine;
    19. using System.Collections;
    20.  
    21. public class scene2mainscript : MonoBehaviour{
    22.  
    23. public CubeNameScript cubeScript;
    24.  
    25. void Awake()
    26. {  
    27.  cubeScript = GameObject.Find("CubeTest").GetComponent<CharacterNameScript>();
    28. }
    29.  
    30.    void OnGUI () {
    31.      GUILabel(new Rect((Screen.width/2)-33, Screen.height-280, 200, 20), cubeScript.CharNameString);
    32.         }
    33. }
    34.  
    In the Unity Editor you need to drag and drop the Cube GameObject in your scene onto the cubetest field in the Inspector.
     
    Last edited: Dec 6, 2014
  5. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    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.