Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

PlayerPrefsX save/load

Discussion in 'Scripting' started by MICKDOii, Jun 14, 2016.

  1. MICKDOii

    MICKDOii

    Joined:
    Jan 12, 2015
    Posts:
    63
    I've manage to write up enough to allow me to save the gameobjects activity (if the gameobjects are active)
    but i'm having some trouble with the PlayerPrefsX.SetVector3Array and PlayerPrefsX.SetQuaternionArray.

    It's hard to find any sort of information I personally can get help from so I'm seeking help here.

    Here is where I got my information and the PlayerPrefsX.cs
    http://wiki.unity3d.com/index.php/ArrayPrefs2


    and here is my script.
    any help would be great.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class S_GameObject : MonoBehaviour {
    5.     [Header("SaveName")]
    6.     //save gameObject isActive?
    7.     public string objACT;
    8.  
    9.  
    10.     //save gameObject position & rotation
    11.     public string objPOS;
    12.     public string objROT;
    13.  
    14.  
    15.  
    16.  
    17.  
    18.     [Space(10)]
    19.     [Header("TRAPS")]
    20.  
    21.     //objects to save
    22.     public GameObject[] traps;
    23.  
    24.  
    25.  
    26.     [Space(10)]
    27.     [Header("ACTIVITY")]
    28.  
    29.     // bool for isActive
    30.     public int[] onOFF;
    31.  
    32.  
    33.  
    34.  
    35.     public void SaveIt()
    36.     {
    37.      
    38.          
    39.  
    40.  
    41.    
    42.         //Save Activity
    43.  
    44.         PlayerPrefsX.SetIntArray(objACT, onOFF);
    45.  
    46.         //Save Pos/Rot
    47.  
    48.         PlayerPrefsX.SetVector3Array(objPOS, traps[]);
    49.         PlayerPrefsX.SetQuaternionArray(objROT, traps[]);
    50.  
    51.  
    52.     }
    53.  
    54.  
    55.  
    56.  
    57.  
    58.     public void LoadIt()
    59.     {
    60.  
    61.      
    62.             onOFF = PlayerPrefsX.GetIntArray(objACT);
    63.      
    64.  
    65.     }
    66.  
    67.  
    68.     // Update is called once per frame
    69.     void Update () {
    70.  
    71.         //SAW
    72.         if(onOFF[0] == 1)
    73.         {
    74.  
    75.             traps[0].SetActive(true);
    76.  
    77.         }
    78.  
    79.         if (onOFF[0] == 0)
    80.         {
    81.  
    82.             traps[0].SetActive(false);
    83.  
    84.         }
    85.  
    86.         //SAW1
    87.  
    88.         if (onOFF[1] == 1)
    89.         {
    90.  
    91.             traps[1].SetActive(true);
    92.  
    93.         }
    94.  
    95.         if (onOFF[1] == 0)
    96.         {
    97.  
    98.             traps[1].SetActive(false);
    99.  
    100.         }
    101.  
    102.  
    103.         //SAW2
    104.  
    105.         if (onOFF[2] == 1)
    106.         {
    107.  
    108.             traps[2].SetActive(true);
    109.  
    110.         }
    111.  
    112.         if (onOFF[2] == 0)
    113.         {
    114.  
    115.             traps[2].SetActive(false);
    116.  
    117.         }
    118.  
    119.  
    120.         //SAW3
    121.  
    122.         if (onOFF[3] == 1)
    123.         {
    124.  
    125.             traps[3].SetActive(true);
    126.  
    127.         }
    128.  
    129.         if (onOFF[3] == 0)
    130.         {
    131.  
    132.             traps[3].SetActive(false);
    133.  
    134.         }
    135.  
    136.  
    137.         //SAW4
    138.  
    139.         if (onOFF[4] == 1)
    140.         {
    141.  
    142.             traps[4].SetActive(true);
    143.  
    144.         }
    145.  
    146.         if (onOFF[4] == 0)
    147.         {
    148.  
    149.             traps[4].SetActive(false);
    150.  
    151.         }
    152.  
    153.  
    154.         //SAW5
    155.  
    156.         if (onOFF[5] == 1)
    157.         {
    158.  
    159.             traps[5].SetActive(true);
    160.  
    161.         }
    162.  
    163.         if (onOFF[5] == 0)
    164.         {
    165.  
    166.             traps[5].SetActive(false);
    167.  
    168.         }
    169.  
    170.  
    171.         //SAW6
    172.  
    173.         if (onOFF[6] == 1)
    174.         {
    175.  
    176.             traps[6].SetActive(true);
    177.  
    178.         }
    179.  
    180.         if (onOFF[6] == 0)
    181.         {
    182.  
    183.             traps[6].SetActive(false);
    184.  
    185.         }
    186.  
    187.  
    188.         //SAW7
    189.  
    190.         if (onOFF[7] == 1)
    191.         {
    192.  
    193.             traps[7].SetActive(true);
    194.  
    195.         }
    196.  
    197.         if (onOFF[7] == 0)
    198.         {
    199.  
    200.             traps[7].SetActive(false);
    201.  
    202.         }
    203.  
    204.  
    205.         //SAW8
    206.  
    207.         if (onOFF[8] == 1)
    208.         {
    209.  
    210.             traps[8].SetActive(true);
    211.  
    212.         }
    213.  
    214.         if (onOFF[8] == 0)
    215.         {
    216.  
    217.             traps[8].SetActive(false);
    218.  
    219.         }
    220.  
    221.  
    222.         //SAW9
    223.  
    224.         if (onOFF[9] == 1)
    225.         {
    226.  
    227.             traps[9].SetActive(true);
    228.  
    229.         }
    230.  
    231.         if (onOFF[9] == 0)
    232.         {
    233.  
    234.             traps[9].SetActive(false);
    235.  
    236.         }
    237.  
    238.  
    239.  
    240.  
    241.  
    242.  
    243.  
    244.  
    245.     }
    246.  
    247.     }
    248.  
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    5,474
    You need to look closer at the script you are using on that site. You're setting up an array of gameobjects called traps[], but neither of those methods takes an array of gameobjects.
     
  3. MICKDOii

    MICKDOii

    Joined:
    Jan 12, 2015
    Posts:
    63
    Not to sure I understand what you mean.
    Could you be referring to using "foreach".
     
  4. Zenix

    Zenix

    Joined:
    Nov 9, 2009
    Posts:
    210
    On lines 48 and 49, you're passing an array of gameobjects to the methods PlayerPrefsX.SetVector3Array and PlayerPrefsX.SetQuaternionArray...
     
  5. MICKDOii

    MICKDOii

    Joined:
    Jan 12, 2015
    Posts:
    63
    Yes, I'm trying to figure out how I would save these.
    I can save the other arrays i'm just having trouble saving vector3array & QuarernionArray.

    I've tried having

    public GameObject[] traps;


    foreach(GameObject t in traps)

    PlayerPrefsX.SetVector3Array(objPOS, t.transform.position);
    PlayerPrefsX.SetQuaternionArray(objPOS, t.transform.rotation);


    but have had no luck.
     
    Last edited: Jun 14, 2016
  6. Zenix

    Zenix

    Joined:
    Nov 9, 2009
    Posts:
    210
    You don't have an array of vector3s, nor an array of quaternions... you have an array of objects. Are you trying to save the positions and rotations for all of these objects?
     
  7. MICKDOii

    MICKDOii

    Joined:
    Jan 12, 2015
    Posts:
    63
    Ah I see, yes i'm trying to save both position and rotation for all the gameobjects in the traps array
     
  8. Zenix

    Zenix

    Joined:
    Nov 9, 2009
    Posts:
    210
    PlayerPrefsX.SetVector3Array("positions", gameObjects.Select(go => go.transform.position).ToArray() );

    Something like that should work.
    You'll need 'Using System.Linq' at the top of the code file.
     
  9. MICKDOii

    MICKDOii

    Joined:
    Jan 12, 2015
    Posts:
    63
    Okay that looks about right but more troubling would be to load something like that.
    would i just use

    traps = PlayerPrefsX.GetVector3Array(objPOS);


    Update: Receiving these errors

    error CS0135: `traps' conflicts with a declaration in a child block

    error CS1502: The best overloaded method match for `PlayerPrefsX.SetVector3Array(string, UnityEngine.Vector3[])' has some invalid arguments

    error CS1503: Argument `#2' cannot convert `object' expression to type `UnityEngine.Vector3[]'

    error CS0135: `traps' conflicts with a declaration in a child block

    error CS1502: The best overloaded method match for `PlayerPrefsX.SetQuaternionArray(string, UnityEngine.Quaternion[])' has some invalid arguments

    error CS1503: Argument `#2' cannot convert `object' expression to type `UnityEngine.Quaternion[]'
     
  10. Zenix

    Zenix

    Joined:
    Nov 9, 2009
    Posts:
    210
    You're really going to want to go through a few c# tutorials before trying to make a game. Those errors should be pretty trivial to fix once you're a bit more familiar with programming :)
     
  11. MICKDOii

    MICKDOii

    Joined:
    Jan 12, 2015
    Posts:
    63

    I have gone through a few. I can write well I just don't have a great knowledge of how to solve all errors, especially something that i'm currently learning.
    But thank you for the help you did give me I appreciate it.
    =]
     
  12. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    5,474
    Thanks Zenix, it was late, so I probably wasn't as clear last night.

    MICKDOii, if you've done tutorials, then you may have only done super basic ones. As Zenix said, those errors are easy to understand. From what I'm seeing, the script you are trying to use may be more advanced than your current level of knowledge. Even if you learn to save the info, you have to learn how to retrieve and apply the info back to your gameobjects.

    That page does give some examples on how to do this, but the concern is you'll just keep getting stuck, which will frustrate you and you'll never finish a project. So this is sound advice to help you.
     
  13. MICKDOii

    MICKDOii

    Joined:
    Jan 12, 2015
    Posts:
    63
    It's okay, I worked it out in the end.
    I assure you my knowledge consist more then drawing a blank on a couple errors from lack of sleep.
    But i understand the concern.
    Thanks again. =]
     
    Zenix likes this.
unityunity