I need an example of how to store a gameObject using playerprefs, and also how to access said object later. I could sit here forever reading links to references, I just learn better by seeing actual examples. Oh kind soul thank you in advance and may your programming fingers be swift and accurate!!!
4 Answers
4Using PlayerPrefs you can store Integers, floats and strings.
This means you cant save the actual GameObect, but you can save, for example a string that you can later parse to a loading script. You can save the transforms position of a gameobject for example.
What I’m trying to get at here is, you can’t do this:
PlayerPrefs.SaveObject(mygameobject)
but, you can save the individual component attributes, such as the position:
PlayerPrefs.Setfloat("xpos", transform.position.x)
then load the position using something like:
function LoadPlayer()
{
transform.position.x = PlayerPrefs.GetFloat (xpos);
//do the rest for Y and Z
}
Of course, there are other (and better) ways of doing this, but it’s the basics.
That’s just the position, you will probably need to save variable states as well, you can apply the same logic as for the position (example):
function LoadPlayer()
{
transform.position.x = PlayerPrefs.GetFloat (xpos);
// do the rest for y and z
Player.myIntVariable = PlayerPrefs.GetInt (myIntSave);
// myIntVariable is a variable in the Player class, saved using PlayerPrefs.SetInt
}
To further the discussion, there are better ways of saving things than using PlayerPrefs, like XML for example. I’ve even spotted a few people who have created there own PlayerPrefs class (i even saw one for free!). Search is your friend here :).
,I create 2 C# scripts, one for the main camera and one for the gameobject (circle2d collider atached to and triggered)
for the camera script named LoadPlayer //this is important in the second script
public GameObject currentCheckpoint;
public float pointy;
public float pointx;
public Movement Player;
void Start () {
Player = FindObjectOfType<Movement> ();
pointx = PlayerPrefs.GetFloat("CheckPointSelectorX");
pointy = PlayerPrefs.GetFloat("CheckPointSelectorY");
Player.transform.position = new Vector3 (pointx, pointy, 1f);
}
void Update () {
if (pointx <= 0) //I use this because at game first start the values are 0 so the player don't show.
{
Player.transform.position = new Vector3 (2f,3f, 1f);//put the value where you want to respawn the player first time
}
And for the script what is atached to the game object with collider on
public LoadPlayer loadPlayer;//or what are called your first script
public float positionx;
public float positiony;
void Start () {
loadPlayer = FindObjectOfType<LoadPlayer> ();
}
void OnTriggerEnter2D(Collider2D other){
if (other.name == "Player") {
loadPlayer.pointx = gameObject.transform.position.x;
loadPlayer.pointy = gameObject.transform.position.y;
positionx = loadPlayer.pointx;
positiony = loadPlayer.pointy;
PlayerPrefs.SetFloat ("CheckPointSelectorX",positionx);
PlayerPrefs.SetFloat ("CheckPointSelectorY",positiony);
PlayerPrefs.Save ();
}
Hope it’s working for you too!
Yes,but maybe someone uses this code because he can't use the other in his game or scene and he needed another solution
– EdwinkePlayerPrefs can save data of type Int, Float, String only. So directly its not possible to save GameObject using PlayerPrefs. Instead you can serialize by storing its individual property values in PlayerPrefs and reading it back while deserializing.
However you can save the trouble, by using Runtime Serialization for Unity. Its not just another serialization plugin which works on custom c# objects. But what makes it special is its capablity to serialize Unity Objects like GameObject, MonoBehaviours, Textures, Prefabs etc. As a matter of fact, you can even use it for Scene Serialization. For more info about supported list, please check this 2.
Here is another way that might work. You can create another script. Then, create the gameobject that you want in that script. Then, get script of gameobject. Go from there. You may have to use the events bar and get method from there.
For example, get the button that you want. Click on the button. Go to the inspector. Drag the button on it. Drag your script on button not on inspector. Then, see the function probably says no function. Then, find your script and look for your method.
I am a beginner coder, too. Hope this helps anyone!
Or
For example,
gameobject.GetComponent(YourScriptWithTheChosenGameObjectInIt).ChosenMethodWithGameObjectInScript();
Here is a link that might help.
https://forum.unity.com/threads/getcomponent-script.25066/
Please let me know if this is the wrong way!
I just ended up making my own save load system with player prefs. even managed a way to load up which weapons and armor that was equipped betwen scenes. This example helped alot. I just didnt' realize that PlayerPrefs simply store a value for later use.
– TheEmeralDreamer