PlayerPrefsX save/load

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.

using UnityEngine;
using System.Collections;

public class S_GameObject : MonoBehaviour {
    [Header("SaveName")]
    //save gameObject isActive?
    public string objACT;


    //save gameObject position & rotation
    public string objPOS;
    public string objROT;



 

    [Space(10)]
    [Header("TRAPS")]

    //objects to save
    public GameObject[] traps;



    [Space(10)]
    [Header("ACTIVITY")]

    // bool for isActive
    public int[] onOFF;
  

  

    public void SaveIt()
    {
     
         


    
        //Save Activity

        PlayerPrefsX.SetIntArray(objACT, onOFF);

        //Save Pos/Rot

        PlayerPrefsX.SetVector3Array(objPOS, traps[]);
        PlayerPrefsX.SetQuaternionArray(objROT, traps[]);


    }





    public void LoadIt()
    {

     
            onOFF = PlayerPrefsX.GetIntArray(objACT);
      

    }


    // Update is called once per frame
    void Update () {

        //SAW
        if(onOFF[0] == 1)
        {

            traps[0].SetActive(true);

        }

        if (onOFF[0] == 0)
        {

            traps[0].SetActive(false);

        }

        //SAW1

        if (onOFF[1] == 1)
        {

            traps[1].SetActive(true);

        }

        if (onOFF[1] == 0)
        {

            traps[1].SetActive(false);

        }


        //SAW2

        if (onOFF[2] == 1)
        {

            traps[2].SetActive(true);

        }

        if (onOFF[2] == 0)
        {

            traps[2].SetActive(false);

        }


        //SAW3

        if (onOFF[3] == 1)
        {

            traps[3].SetActive(true);

        }

        if (onOFF[3] == 0)
        {

            traps[3].SetActive(false);

        }


        //SAW4

        if (onOFF[4] == 1)
        {

            traps[4].SetActive(true);

        }

        if (onOFF[4] == 0)
        {

            traps[4].SetActive(false);

        }


        //SAW5

        if (onOFF[5] == 1)
        {

            traps[5].SetActive(true);

        }

        if (onOFF[5] == 0)
        {

            traps[5].SetActive(false);

        }


        //SAW6

        if (onOFF[6] == 1)
        {

            traps[6].SetActive(true);

        }

        if (onOFF[6] == 0)
        {

            traps[6].SetActive(false);

        }


        //SAW7

        if (onOFF[7] == 1)
        {

            traps[7].SetActive(true);

        }

        if (onOFF[7] == 0)
        {

            traps[7].SetActive(false);

        }


        //SAW8

        if (onOFF[8] == 1)
        {

            traps[8].SetActive(true);

        }

        if (onOFF[8] == 0)
        {

            traps[8].SetActive(false);

        }


        //SAW9

        if (onOFF[9] == 1)
        {

            traps[9].SetActive(true);

        }

        if (onOFF[9] == 0)
        {

            traps[9].SetActive(false);

        }








    }

    }

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.

Not to sure I understand what you mean.
Could you be referring to using “foreach”.

On lines 48 and 49, you’re passing an array of gameobjects to the methods PlayerPrefsX.SetVector3Array and PlayerPrefsX.SetQuaternionArray…

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.

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?

Ah I see, yes i’m trying to save both position and rotation for all the gameobjects in the traps array

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.

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[ ]’

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 :slight_smile:

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.
=]

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.

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. =]

1 Like