Usage on PlayerPrefs?

Hello,

How do I use PlayerPrefs? I want it so that I can select different characters, and whichever one I click be loaded into my scene/level. I’ve looked for almost a month, and cannot find anything on it. :face_with_spiral_eyes:

You are going to use the int one.

BIkebreck

Thanks for that. Now, How do I set it so that I can select a car transform?

 var Car_1 : Transform;

PlayerPrefs.SetInt("Car 1", Car_1);

And, How do I make it so that when I click it, it loads that car into the scene. GetInt? But, How do I use SetInt when clicked?

you can’t store any transforms or alike, only simple data. you then have to interpret these data to do the same selection in a later case again.
easiest is a global monobehaviour with an array of transforms which are always there and then just store the index on this array for example.
alternatively store them in a dictioanry<string,Transform> and then store the string (key / name) - or the name of the transform directly if you use the name to instantiate it from resources for example

uhh. That confused me a little. How do I set up an array for transforms?

Thanks. Now , How would I set a car to an Int/something?

You can’t set a car to an int/something (whatever that means :wink:

PlayerPrefs offers basic, low-level functionality. You can save and retrieve:

  • Integers (whole numbers like 37, -104, etc.)
  • Floats (numbers that can have fractional parts, like 4.26234)
  • Strings

Anything else you do has to be expressed in terms of those basic primitives. The details are completely up to you. Want to save which car the player currently has selected? Save an integer that indexes into a container containing the car prefabs. Or maybe the name of a game object representing the car in question. It’s up to you.

(I think there are some scripts on the wiki that wrap PlayerPrefs and provide some additional functionality, but I don’t know the details off the top of my head.)

I want to do one of those. Which is easier? I thought of using a GUIButton to click on, and that tells what car you selected. How would I do that?

Hello carking1996; do you have any knowledge about scripting? i’m asking because here we got two things:
1- how to use PlayerPrefs
2- what do you need to perform save/load from/to scene stuffs.

First point has been answered.
Second point exceeds (?) the topic.

I suggest you to read some basic “how to develop” tutorial.

Hi Kokumo, I have basic understanding of scripting, and can write scripts. I have just never used PlayerPrefs before. So, therefore, I don’t know how to use them. :wink:

In the menu:

var pos : Rect;

function OnGUI () {

    if (GUI.Button (pos, "Car 1")) {
        PlayerPrefs.SetString("Car Name", "Car1");
    }

}

In the game:

function Start () {

    var obj : GameObject = Instantiate (Resources.Load (PlayerPrefs.GetString("Car Name")), Vector3.zero, Quaternion.identity);

}

Just make sure you have your car prefabs in the Resources folder.

Cool. Thanks! Now, where do I place the 2nd script? In an empty GameObject? I’ll try that. :slight_smile:

EDIT: I have a button. It says, “Car 1”. Now, what do I do next? Should I make it so that when I click that button, it loads the level? How do I make it so that I select the cars?

You would replace however you were Instantiating your car beforehand with the 2nd script. The only difference is that mine is loading from the Resources folder. (you would probably want to change where its instantiating too. I have it being created at (0, 0, 0))

You could do that if it is the functionality you are looking for.

The only piece of code that you would need to add (anywhere) to select your car is

PlayerPrefs.SetString("Car Name", "Car1");

Where “Car1” is replaced with the name of the selected car.

  1. Before, I wasn’t instantiating it. I just placed it there. :confused:

  2. Yes.

  3. What do you mean, the name of the selected car? I don’t have the names of a car anywhere in the screen. Do I replace ‘Car1’ with the name of my first car? Then make another for Car2? Then, how does it know which is which?

The name of the car should be the name of the prefab for that car. (In Dman’s example at least.)

Yes. I know that. But, how does it know which car I clicked(multiple cars…) ?

You have a prefab for you car correct? That prefab has a name. It is what you replace “Car1” with.

You change the value of “Car Name” to the name of the prefab you would like to Instantiate.

It would know which car you clicked because you set the value of “Car Name” in the PlayerPrefs to the name of the prefab when you select the car.

The question isn’t really clear, but generally, to determine when an object has been clicked on you’d use Physics.Raycast() or OnMouseDown(), or perform a simple screen-space check.