Save Component Array to Player Prefs.

I have a gameobject with several child objects that carry the component. Some are enabled some arent. The ones that are enabled are in an array. Basically im trying to figure out the best way to save them to player prefs.
.
.
.

  1. Edit

***I solved it… Solution is below. FYI you must first search google for playerprefsX, find the script on github, import that script into your project and then you can access the playprefsX class. Hope this helps someone!

public component[] allComponents;
public list<string> componentNames;

public void Save()
{

    foreach (component c in allComponents)
    {
        if (c.isenabled)
        {
            if (!componentNames.Contains(c.name))
                componentNames.Add(c.name);
        }
        else
        {
            if (componentNames.Contains(c.name))
            {
               componentNames.Remove(c.name);
            }
        }

    }
    saveArray= componentNames.ToArray();
    PlayerPrefsX.SetStringArray("savedComponents", saveArray);
    

}

public void Load()
{
    
        foreach (component c in allComponents)
        {
            c.isenabled = false;
        }
            
            loadedArray = PlayerPrefsX.GetStringArray("savedComponents");
        foreach (string s in loadedArray)
        {
            foreach (component c in allComponents)
            {

                if (c.name == s)
                {
                    c.isenabled = true;
                }
            }
        }
    

}

Hiya!

Well, if we check Unity Docs for PlayerPrefs, we can see that we can save float, ints, and strings. Not components sadly. However, if I were to be trying to do this, I’d find a way to use strings. We could potentially grab the name of each component that we want saved, create a string of their names like this:

string componentString = "BoxCollider, Rigidbody, MyScript1, MyScript2";
PlayerPrefs.SetString(key, componentString);

Later, when you want to grab the components again, you can use PlayerPrefs.GetString() to get that string of components, then split it to grab the individual component names. At this point, you’d have an array of strings with all of those components you saved. I’m not sure what you want to do with them now, but that’s probably how I’d go about this. Converting the strings back into a type though may be interesting… It looks like there is Type.GetType(), but that may not quite give you what you’re looking for. I haven’t tested this.

Good day.

What do you want to save?
With PlayerPrefs, You can only set Int, Float and Strings.