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

Question Confusing up public/public static/private, and how to use transport data across save states

Discussion in 'Scripting' started by Cratthorax, Dec 31, 2020.

  1. Cratthorax

    Cratthorax

    Joined:
    Oct 18, 2020
    Posts:
    24
    Good day,

    I do understand the documented difference. Public is unique and shared only inside the class, while public static IS shared across the project as global. Private is none of the initial two, and serves as protected "hidden" reference, right?. The problem for me is: 1. best practice, and 2. why declare a private, if no declaration in front of a reference does the same job, like so:

    Code (CSharp):
    1. private Rect myRect;
    2. Rect myRect; <-- difference to private, I fail to see...
    I'm specifically asking because I get all sorts of trouble trying to save game data across sessions, utilizing a mixture of PlayerRef, and external .json procedure. To get even more into detail, I have a hard time to save float coordinates, for a gameObject(Texture2D)'s transform.position and rotation, as well as instantiated prefabs, which I would usually catch with an
    Code (CSharp):
    1. for(var i = 0 ; i < gameObjects.Length ; i ++)
    loop.

    Trying to reload this data always results in broken coordinates, because I'd also need to take mouse position vs world position data into account. On a 2d game btw.

    So what I'm really asking. What is the get to go recommended best practice, and most code effective way to save data between game sessions?
     
  2. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,357
    It's not clear to me why you're supposing these problems have anything to do with whether the data is public, private or static. If you're having problem with your save/load script you could probably get help fixing it, if you post the code and explain more precisely what the problem is.
     
    Vryken likes this.
  3. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    Access Modifiers (the public/private thing) only determine how a member of a class can be accessed.
    • public
      - The member can be accessed outside of the class, such as from a different script.
    • private
      - The member can only be accessed inside the class. Other scripts cannot access it.
    • protected
      - The member can be accessed within the class and by any other classes than inherit from it.
    static
    isn't an access modifier, but it does essentially do what you described - make the member globally accessable from the project without being dependent on an object existing.

    But like @kdgalla mentioned, these things don't directly affect how code is executed.
    int x = 5
    will still be
    int x = 5
    regardless if it's public/private/protected/static/etc.
     
    Ardenian likes this.
  4. seejayjames

    seejayjames

    Joined:
    Jan 28, 2013
    Posts:
    685
    In Unity,
    private int myInt = 5;
    is the same as
    int myInt = 5;
    "private" is for readability only and is not required. "public", "protected", and "static" are not assumed and must be included as needed.

    There's also
    [SerializeField]
    private int myInt = 5;
    This makes a private int (can't be accessed by other classes), but will show up in the Inspector for fiddling purposes :)
     
  5. Cratthorax

    Cratthorax

    Joined:
    Oct 18, 2020
    Posts:
    24
    Thanks for the response guys.

    Hmm, so in return it's best practice to not use any access modifiers, unless you really need them, to save code bloat?

    I'm afraid that is not a got idea, posting thousands of lines of code, that cross-connect in between multiple scripts. I was asking if maybe the wrong usage of access modifiers, is causing the issue. But I deem not. I'm trying my luck with coroutine and/or lateUpdate now.

    But if you really feel like breaking your head, here you go:

    Code (csharp):
    1.  
    2.     void logManager(int windowID)
    3.     {
    4.         toggleLogArea = GUI.Toggle(new Rect(2, 2.5f, 15, 15), toggleLogArea, string.Empty);
    5.         if (toggleLogArea) {
    6.             logToSave = logString;
    7.             homeToSave = homeString;
    8.             constPosXToSave = constPosXFloat;
    9.             constPosYToSave = constPosYFloat;
    10.             constPosZToSave = constPosZFloat;
    11.             if (GUI.Button(new Rect(Screen.width / 2 - 850, 2.5f, 100, 21), "Save Data"))
    12.                 saveLog();
    13.             if (GUI.Button(new Rect(Screen.width / 2 - 750, 2.5f, 100, 21), "Load Data")){
    14.                 loadConst();
    15.                 loadLog();
    16.             }
    17.             logRect.size = new Vector2 (1007, 845);
    18.             logLabelString = GUI.TextField (new Rect (18, 2.5f, 85.5f, 21), logLabelString, 15);
    19.             if(logString != null){
    20.                 logString = GUI.TextArea (new Rect (2.5f, 25, 1000.5f, 816.5f), logString, 7000);
    21.             } else if(logString != null){
    22.                 logString = GUI.TextArea (new Rect (2.5f, 25, 1000.5f, 816.5f), "", 7000);
    23.             }
    24.         } else {
    25.             logRect.size = new Vector2 (35, 27);
    26.             //GUI.DragWindow(new Rect(15, 0, 92, 27));//dragable window, but removed it for release to prevent bugs
    27.         }
    28.     }
    29.  
    30.     public static void saveLog()
    31.     {
    32.         if(toggleHome == true){
    33.             homeString = main.homeStar.GetComponent<system> ().name;
    34.         }
    35.         if(toggleHome == false){
    36.             homeString = null;
    37.         }
    38.         if(toggleHome == true && main.homeStar.GetComponent<system> ().name == "Sol"){
    39.             homeString = "Sol";
    40.         }
    41.         constPosXFloat = constPos.x;
    42.         constPosYFloat = constPos.y;
    43.         constPosZFloat = constPos.z;
    44.         PlayerPrefs.SetString("SavedLog", logString);
    45.         PlayerPrefs.SetString("SavedHome", homeString);
    46.         PlayerPrefs.SetFloat("ConstPosX", constPosXFloat);
    47.         PlayerPrefs.SetFloat("ConstPosY", constPosYFloat);
    48.         PlayerPrefs.SetFloat("ConstPosZ", constPosZFloat);
    49.         PlayerPrefs.Save();
    50.         Log("Data saved!");
    51.     }
    52.     public static void loadConst()
    53.     {
    54.         constPosXToSave = PlayerPrefs.GetFloat("ConstPosX");
    55.         constPosYToSave = PlayerPrefs.GetFloat("ConstPosY");
    56.         constPosZToSave = PlayerPrefs.GetFloat("ConstPosZ");
    57.         constPos.x = constPosXFloat;
    58.         constPos.y = constPosYFloat;
    59.         constPos.z = constPosZFloat;
    60.         constPosXFloat = constPosXToSave;
    61.         constPosYFloat = constPosYToSave;
    62.         constPosZFloat = constPosZToSave;
    63.     }
    64. //done the extra loadConst blog, since it would mess up with isnide the original load block
    65.     public static void loadLog()
    66.     {
    67.         SetCursorPos(Screen.width/2, Screen.height/2);
    68.         if (PlayerPrefs.HasKey("SavedLog"))
    69.         {
    70.             logToSave = PlayerPrefs.GetString("SavedLog");
    71.             homeToSave = PlayerPrefs.GetString("SavedHome");
    72.             logString = logToSave;
    73.             GameObject[] star = GameObject.FindGameObjectsWithTag ("star_system");
    74.             for (var i = 0; i < star.Length; i ++) {
    75.                 if(star[i] && homeToSave != null){
    76.                     if(star[i].name == homeToSave){
    77.                         homeStar = star[i];
    78.                     } else if(star[i].name != homeToSave){
    79.                         return;
    80.                     }
    81.                     homeStar.gameObject.GetComponent<system>().select(true);
    82.                     homeStar.gameObject.GetComponent<system>().select(false);
    83.                     MouseEvent(main.MouseEventFlags.RightUp | main.MouseEventFlags.RightDown);
    84.                     system.loadSelection = false;
    85.                     homeStar.name = homeString;
    86.                 } else if(!star[i]){
    87.                     Log ("No Home Star set!");
    88.                 }
    89.             }
    90.             homeString = homeToSave;
    91.             return;
    92.         }
    93.         else
    94.             Log("There is no log data!");
    95.     }

    The coordinate problem might happen due to an extremely weird
    Code (CSharp):
    1. Camera.main
    setup.

    I'm working on an inherited Editor for Stellaris, made with 4.7.1(which I have to stay with, since updating to latest Unity would be a devastating pita). I'm using two cameras, one as top down 2D on a 3D setup -don't ask me why, the org creator did it for a reason? I don't know-, and one camera as static background projector for a background sprite. I would utilize code like this, to get correct coordinates for objects placed in the game world:

    Code (csharp):
    1.  
    2.             mousePos.x = Input.mousePosition.x;
    3.             mousePos.y = cam.pixelHeight - Input.mousePosition.y; //cam.pixelHeight is used to make mouse position 0,0 at the top left corner
    4.             mousePoint = cam.ScreenToWorldPoint(new Vector3(mousePos.x, mousePos.y, 460)); //default 3rd variable ---> cam.nearClipPlane; 460 is used for correct coordinates, see upside!
    5.  
    The problem is, it can't be utilized when trying to save coordinate data from a dragable Texture2d(the one with the messed up coordinate save date), since I would need dynamic position/rotation code, for an object placed in the game world, moved by a mouse pointer which I catch with a Physics.Raycaster, and then needs to be transformed to that weird 2D/3D setup. So while I get the exact raycaster data on the mouse pointer, I can never exactly pinpoint the coordinate data, for a dragged object after repositioning/rerotating it. And yes, I was using euler, delta, event strategys a lot. None of them worked.

    I verified I save/load the correct Data via PlayerPrefs, but the Texture2D object will never end up in the position it gets feeded with, unless I didn't rotated it.

    The Camera.main is "sliding" on a CameraAxe, which uses the middle of the game map as anchor point, and it is rotated by x:90, y:180, z:0. It also sits on z:460, hence why you see the number 460 in any of my position definition code.

    Ain't it complicated? Happy new year...:)
     
    Last edited: Jan 1, 2021
  6. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,357
    I still don't understand what it is that your saving. Is it a position of an object that you placing at runtime? Is it a Unity gameObject?
     
  7. Cratthorax

    Cratthorax

    Joined:
    Oct 18, 2020
    Posts:
    24
    It's an external .png imported at runtime, so users are allowed to define their own background image. I would save the coordinates of the, at runtime created sprite, that houses the Texture2D.
     
  8. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,357
    Do you actually need to save the position? Is it not something you can recreate? How do you determine the position to begin with?

    Is the runtime-created-sprite a GameObject? Can you show the code where you retrieve the position from the objects transform and where you restore the loaded position to the objects transform?
     
  9. Cratthorax

    Cratthorax

    Joined:
    Oct 18, 2020
    Posts:
    24
    That's what I do. It gets messed up whenever I try to give it a custom position, but only when saving. It works perfectly as long as I stay inside the game, even when saving/reloading. Once I exit the game, and return, the position is not were it is supposed to be, despite Debug.Log perfectly gives me the correct saved position data. It just doesn't apply it correctly, and I don't know why?

    Code to apply constellation sprite:

    Code (csharp):
    1.  
    2.     void applyConstellations()
    3.     {
    4.         if (constellationSprite.gameObject.GetComponent<SpriteRenderer>().enabled == false)
    5.         {
    6.             constellationSprite.gameObject.GetComponent<SpriteRenderer>().enabled = true;
    7.             texConst = LoadBackgroundPNG("imageConst.png");
    8.             Sprite sprite = Sprite.Create(texConst, new Rect(0, 0, texConst.width, texConst.height), Vector2.zero);
    9.             constellationSprite.sprite = sprite;
    10.             if(homeStar != null && homeStar.GetComponent<system>().systemName == "Sol"){
    11.                 constellationSprite.gameObject.GetComponent<RectTransform>().position = new Vector3(541+homeStar.transform.position.x, homeStar.transform.position.y, 479+homeStar.transform.position.z);
    12.                 //Log (homeStar.transform.position.ToString());
    13.             } else if(homeStar == null){
    14.                 constellationSprite.gameObject.GetComponent<RectTransform>().position = new Vector3(500, 0, 500);
    15.             }
    16.             Log ("Constellations Applied");
    17.         }
    18.      
    19.         else if (constellationSprite.gameObject.GetComponent<SpriteRenderer>().enabled == true)
    20.         {
    21.             constPos = constellationSprite.gameObject.GetComponent<RectTransform>().position = new Vector3(constPos.x, constPos.y, constPos.z);
    22.             constellationSprite.gameObject.GetComponent<SpriteRenderer>().enabled = false;
    23.             DestroyImmediate(texConst);
    24.             Log ("Constellations Removed");
    25.         }
    26.     }

    And for the drag logic:

    Code (csharp):
    1. //in OnGUI()
    2.         //dragBox overlay for Constellation Manager
    3.         dragBox = new Rect (0, 0, Screen.width, Screen.height);
    4.         constellationManager ();
    5.  
    6. //drag, position, rotation, zooming logic etc.
    7.     void constellationManager(){
    8.         if (Input.GetKey (KeyCode.F10)) {
    9.             currentMode = 8;
    10.             //GUI.Box (new Rect (dragBoxX, dragBoxY, Screen.width, Screen.height), string.Empty);// visual indicator for debugging
    11.             var e = Event.current;
    12.             if (e.type == EventType.MouseDrag && dragBox.Contains (e.mousePosition) && Input.GetKey (KeyCode.Mouse0)) {
    13.                 dragBox.x += e.delta.x;//makes sure the dragBox stays in position
    14.                 dragBox.y += e.delta.y;
    15.                 constellationSprite.transform.position += new Vector3 (-e.delta.x, 0, e.delta.y);
    16.                 //dragBoxX = rect.x += e.delta.x;//makes sure the dragBox indicator stays in position
    17.                 //dragBoxY = rect.y += e.delta.y;
    18.             }
    19.             if (e.type == EventType.ScrollWheel && dragBox.Contains (e.mousePosition) && Input.GetKey (KeyCode.Mouse1)) {
    20.                 if (Input.GetAxis ("Mouse ScrollWheel") < 0) {
    21.                     dragBox.x += e.delta.x;//makes sure the dragBox stays in position
    22.                     dragBox.y += e.delta.y;
    23.                     if (constellationSprite.transform.position.y <= -1) {
    24.                         constellationSprite.transform.position += new Vector3 (0, 5.0f + moveInc, 0);
    25.                     }
    26.                 }
    27.                 if (Input.GetAxis ("Mouse ScrollWheel") > 0) {
    28.                     dragBox.x += e.delta.x;//makes sure the dragBox stays in position
    29.                     dragBox.y += e.delta.y;
    30.                     if (constellationSprite.transform.position.y >= -530) {
    31.                         constellationSprite.transform.position += new Vector3 (0, -5.0f - moveInc, 0);
    32.                     }
    33.                 }
    34.             }
    35.             if (e.type == EventType.ScrollWheel && dragBox.Contains (e.mousePosition) && !Input.GetKey (KeyCode.Mouse1)) {
    36.                 if (Input.GetAxis ("Mouse ScrollWheel") < 0) {
    37.                     float distance_to_screen = Camera.main.WorldToScreenPoint (transform.position).z;
    38.                     transform.position = Camera.main.ScreenToWorldPoint (new Vector3 (Input.mousePosition.x, Input.mousePosition.y, distance_to_screen));
    39.                     constellationSprite.transform.RotateAround (transform.position, Vector3.up, Time.deltaTime * 90f + moveInc);
    40.                 }
    41.                 if (Input.GetAxis ("Mouse ScrollWheel") > 0) {  
    42.                     screenPoint = Camera.main.WorldToScreenPoint (transform.position);
    43.                     offsetPos = transform.position - Camera.main.ScreenToWorldPoint (new Vector3 (Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
    44.                     constellationSprite.transform.RotateAround (transform.position - offsetPos, Vector3.down, Time.deltaTime * 90f + moveInc);
    45.                 }
    46.             }
    47.         } else if(Input.GetKeyUp (KeyCode.F10)){
    48.             currentMode = 0;
    49.             constPos = constellationSprite.transform.position - offsetPos;
    50.         }
    51.     }
     
  10. seejayjames

    seejayjames

    Joined:
    Jan 28, 2013
    Posts:
    685

    It's not about "bloat", it's simply setting the level of access. Use whatever level you need. In my experience public and private are common, static is more rare but certainly used in a number of cases (maintaining values across scenes or creating utility methods accessible anywhere), and protected is quite rare, as you'd be making a class that inherits from another class---more advanced and often not needed for many projects.
     
  11. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,357
    So position of homeStar is what you're saving and loading? Is that also the value that's incorrect? I don't see how this code connects with the code that you posted earlier. Earlier you are loading the position to something called constPos and constPosXFloat, etc.
     
  12. Cratthorax

    Cratthorax

    Joined:
    Oct 18, 2020
    Posts:
    24
    No, the position of the overlayed constellation image is what I'm saving.
     
  13. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,357
    Well, one thing that jumps-out to me in the code you posted earlier:
    Code (csharp):
    1.  
    2.     public static void loadConst()
    3.     {
    4.         constPosXToSave = PlayerPrefs.GetFloat("ConstPosX");
    5.         constPosYToSave = PlayerPrefs.GetFloat("ConstPosY");
    6.         constPosZToSave = PlayerPrefs.GetFloat("ConstPosZ");
    7.         constPos.x = constPosXFloat;
    8.         constPos.y = constPosYFloat;
    9.         constPos.z = constPosZFloat;
    10.         constPosXFloat = constPosXToSave;
    11.         constPosYFloat = constPosYToSave;
    12.         constPosZFloat = constPosZToSave;
    13.     }
    14.  
    Notice that you're not actually setting the values of constPos to values that you're loading from PlayerPrefs. Are you sure you didn't mean to write:
    Code (csharp):
    1.  
    2. constPos.x = constPosXToSave;
    3.  
    etc?
     
  14. Cratthorax

    Cratthorax

    Joined:
    Oct 18, 2020
    Posts:
    24
    Thanks for your continous patience. I did what you proposed in any constellation possible, just to exclude the options of something being fishy about the code execution sequence. But no luck for me. I got that PlayerRef code from here.

    I have to be honest, the usage of multiple floats replacing each other was confusing to me in the beginning as well, but it turned out it actually's to happen or won't work at all. This is where the "PlayerRef manager" starts in OnGUI():

    Code (csharp):
    1.  
    2.         if (toggleLogArea && !isModKey) {
    3.             logRect = GUI.Window (0, logRect, logManager, "");//prevents double text in write mode
    4.         } else if (!toggleLogArea && !isModKey) {
    5.             logRect = GUI.Window (0, logRect, logManager, "");
    6.             GUILayout.BeginArea(new Rect (logRect.position.x+35, logRect.position.y+2.5f, 85.5f, 21));
    7.             GUILayout.Label (new GUIContent(logLabelString));
    8.             if(/*Input.GetKey(KeyCode.Mouse1)*/Input.GetButtonDown("Unselect") && GUILayoutUtility.GetLastRect ().Contains (Event.current.mousePosition))
    9.             {
    10.                 logRect.position = new Vector2 (430, 40);
    11.             }
    12.             GUILayout.EndArea();
    13.         }
    14.  
    15.     void logManager(int windowID)
    16.     {
    17.         toggleLogArea = GUI.Toggle(new Rect(2, 2.5f, 15, 15), toggleLogArea, string.Empty);
    18.         if (toggleLogArea) {
    19.             logToSave = logString;
    20.             homeToSave = homeString;
    21.             constPosXToSave = constPosXFloat;
    22.             constPosYToSave = constPosYFloat;
    23.             constPosZToSave = constPosZFloat;
    24.             if (GUI.Button(new Rect(Screen.width / 2 - 850, 2.5f, 100, 21), "Save Data"))
    25.                 saveLog();
    26.             if (GUI.Button(new Rect(Screen.width / 2 - 750, 2.5f, 100, 21), "Load Data")){
    27.                 loadConst();
    28.                 loadLog();
    29.             }
    30.             logRect.size = new Vector2 (1007, 845);
    31.             logLabelString = GUI.TextField (new Rect (18, 2.5f, 85.5f, 21), logLabelString, 15);
    32.             if(logString != null){
    33.                 logString = GUI.TextArea (new Rect (2.5f, 25, 1000.5f, 816.5f), logString, 7000);
    34.             } else if(logString != null){
    35.                 logString = GUI.TextArea (new Rect (2.5f, 25, 1000.5f, 816.5f), "", 7000);
    36.             }
    37.         } else {
    38.             logRect.size = new Vector2 (35, 27);
    39.             //GUI.DragWindow(new Rect(15, 0, 92, 27));//dragable window, but removed it for release to prevent bugs
    40.         }
    41.     }

    This is the save data:

    Code (csharp):
    1.  
    2.     public static void saveLog()
    3.     {
    4.         if(toggleHome == true){
    5.             homeString = main.homeStar.GetComponent<system> ().name;
    6.         }
    7.         if(toggleHome == false){
    8.             homeString = null;
    9.         }
    10.         if(toggleHome == true && main.homeStar.GetComponent<system> ().name == "Sol"){
    11.             homeString = "Sol";
    12.         }
    13.         constPosXFloat = constPos.x;
    14.         constPosYFloat = constPos.y;
    15.         constPosZFloat = constPos.z;
    16.         PlayerPrefs.SetString("SavedLog", logString);
    17.         PlayerPrefs.SetString("SavedHome", homeString);
    18.         PlayerPrefs.SetFloat("ConstPosX", constPosXFloat);
    19.         PlayerPrefs.SetFloat("ConstPosY", constPosYFloat);
    20.         PlayerPrefs.SetFloat("ConstPosZ", constPosZFloat);
    21.         PlayerPrefs.Save();
    22.         Log("Data saved!");
    23.     }

    Then I'd divide the constellation load data from the rest, because I'd figure at one point, the coordinates would entirely be messed up, becaause of how OnGUI() and Update() interact. Notice that using LateUpdate() nor using a coroutine changed anything for the better:

    Code (csharp):
    1.  
    2.     public static void loadConst()
    3.     {
    4.         constPosXToSave = PlayerPrefs.GetFloat("ConstPosX");
    5.         constPosYToSave = PlayerPrefs.GetFloat("ConstPosY");
    6.         constPosZToSave = PlayerPrefs.GetFloat("ConstPosZ");
    7.         constPosXFloat = constPosXToSave;
    8.         constPosYFloat = constPosYToSave;
    9.         constPosZFloat = constPosZToSave;
    10.         constPos.x = constPosXFloat;
    11.         constPos.y = constPosYFloat;
    12.         constPos.z = constPosZFloat;
    13.     }

    And the rest:

    Code (csharp):
    1.  
    2.     public static void loadLog()
    3.     {
    4.         SetCursorPos(Screen.width/2, Screen.height/2);
    5.         if (PlayerPrefs.HasKey("SavedLog"))
    6.         {
    7.             logToSave = PlayerPrefs.GetString("SavedLog");
    8.             homeToSave = PlayerPrefs.GetString("SavedHome");
    9.             logString = logToSave;
    10.             GameObject[] star = GameObject.FindGameObjectsWithTag ("star_system");
    11.             for (var i = 0; i < star.Length; i ++) {
    12.                 if(star[i] && homeToSave != null){
    13.                     if(star[i].name == homeToSave){
    14.                         homeStar = star[i];
    15.                     } else if(star[i].name != homeToSave){
    16.                         return;
    17.                     }
    18.                     homeStar.gameObject.GetComponent<system>().select(true);
    19.                     homeStar.gameObject.GetComponent<system>().select(false);
    20.                     MouseEvent(main.MouseEventFlags.RightUp | main.MouseEventFlags.RightDown);
    21.                     homeStar.name = homeString;
    22.                     system.loadSelection = false;
    23.                 } else if(!star[i]){
    24.                     Log ("No Home Star set!");
    25.                     return;
    26.                 }
    27.             }
    28.             homeString = homeToSave;
    29.             return;
    30.         }
    31.         else
    32.             Log("There is no log data!");
    33.     }

    On more detail: The whole load/save procedure is initiated from a dedicated "Project Manager" script, handling the whole PlayerRef/.Json based saving/loading. The Texture2D is imported from an external source, placed in the root folder. This is the code I'm using to toggle it on and off:

    Code (csharp):
    1.  
    2.     void applyConstellations()
    3.     {
    4.         if (constellationSprite.gameObject.GetComponent<SpriteRenderer>().enabled == false)
    5.         {
    6.             constellationSprite.gameObject.GetComponent<SpriteRenderer>().enabled = true;
    7.             texConst = LoadBackgroundPNG("imageConst.png");
    8.             Sprite sprite = Sprite.Create(texConst, new Rect(0, 0, texConst.width, texConst.height), Vector2.zero);
    9.             constellationSprite.sprite = sprite;
    10.             if(homeStar != null && homeStar.GetComponent<system>().systemName == "Sol"){
    11.                 constellationSprite.gameObject.GetComponent<RectTransform>().position = new Vector3(541+homeStar.transform.position.x, homeStar.transform.position.y, 479+homeStar.transform.position.z);
    12.                 //Log (homeStar.transform.position.ToString());
    13.             } else if(homeStar == null){
    14.                 //constellationSprite.gameObject.GetComponent<RectTransform>().position = new Vector3(500, 0, 500);
    15.                 constellationSprite.gameObject.GetComponent<RectTransform>().position = new Vector3(constPos.x, constPos.y, constPos.z);
    16.             }
    17.             Log ("Constellations Applied");
    18.         }
    19.    
    20.         else if (constellationSprite.gameObject.GetComponent<SpriteRenderer>().enabled == true)
    21.         {
    22.             constPos = constellationSprite.gameObject.GetComponent<RectTransform>().position = new Vector3(constPos.x, constPos.y, constPos.z);
    23.             constellationSprite.gameObject.GetComponent<SpriteRenderer>().enabled = false;
    24.             DestroyImmediate(texConst);
    25.             Log ("Constellations Removed");
    26.         }
    27.     }

    So what happens whenever I toggle the constellation is, it will bake the data to its public static reference-which I'm using because I'd call for it from another script as well-. And when I'm actually saving the game, it will save the last baken coordinate to the PlayerRef. Staying with the current game session it all works fine and dandy. Once I exit the game and reload, the coordinates are messed up, like if it would load random coordinates any time I'm starting a new game.
     
  15. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,357
    That was going to be the next thing I wanted to bring up. There appears to be a lot of redundant an unnecessary code in there. You generally don't want to go storing data in multiple duplicate variables because updating one set of variables will not update the others. You end-up with data that is out-of-sync. Having variables in method is ok, but duplicate members like constPosXFloat is just causing confusion. It's not really necessary, but also the name is not descriptive. It doesn't really tell what the purpose of the variable is.

    For example, I don't understand why loadConst
    Code (csharp):
    1.  
    2. public static void loadConst()
    3.     {
    4.         constPosXToSave = PlayerPrefs.GetFloat("ConstPosX");
    5.         constPosYToSave = PlayerPrefs.GetFloat("ConstPosY");
    6.         constPosZToSave = PlayerPrefs.GetFloat("ConstPosZ");
    7.         constPosXFloat = constPosXToSave;
    8.         constPosYFloat = constPosYToSave;
    9.         constPosZFloat = constPosZToSave;
    10.         constPos.x = constPosXFloat;
    11.         constPos.y = constPosYFloat;
    12.         constPos.z = constPosZFloat;
    13.     }
    14.  
    can't just be:
    Code (csharp):
    1.  
    2. public static void loadConst()
    3.     {
    4.         constPos.x = PlayerPrefs.GetFloat("ConstPosX");
    5.         constPos.y = PlayerPrefs.GetFloat("ConstPosY");
    6.         constPos.z = PlayerPrefs.GetFloat("ConstPosZ");
    7.     }
    8.  
    What do you need constPosXFloat and constPosXToSave, etc for?