Save Play Preference in an extern file

first excuse me for my bad english, I’m French, but my English level it’s hight ^^

this is my question/problem :
I wanna save the player settings in a file, to restore it for the other playing time

and I want do that in JS, if it possible (i have never programing in C# ^^)

Thanks for your reading

Use PlayerPrefs.

–Eric

Nice, it’s more perfect than a file

thanks

I have some question :

There are a function that Called at the start of the Application ?
And how call a function that located in an other Script file ?

If you want a startup script, create an empty GameObject in your scene and attach some “StartupScript” code to its Awake and/or Start methods.

Calling functions on other scripts involves using GetComponent and calling the functions directly (preferred) or using SendMessage.

thanks,

but I have some error that I don’t understand:

this is the code on the line :

function Start(){
	var currentGameConfig : GameCFG = gameObject.GetComponent(GameCFG);
	currentGameConfig.getGameConfig();
}

and the code of my GameCFG class :

class GameCFG
{
	//window CFG
	var gameWidth : int;
	var gameHeight : int;
	var gameFullscreen : boolean;
	
	var gameVolume : float;
	

	function 	gameConfig() { //Constructor
		gameWidth = 1024;
		gameHeight = 768;
		gameFullscreen = true;
		gameVolume = 1.0;
	}
	
	function	getGameConfig() {
		if (PlayerPrefs.HasKey("cfg")) { //get stored cfg
			gameWidth = PlayerPrefs.GetInt("gameWidth");
			gameHeight = PlayerPrefs.GetInt("gameHeight");
			if (PlayerPrefs.GetInt("gameFullscreen") == 1) {
				gameFullscreen = true;
			}
			else {
				gameFullscreen = false;
			}
			gameVolume = PlayerPrefs.GetFloat("gameVolume");
		}
		else { //create default cfg
			PlayerPrefs.SetInt("cfg", 1);
			PlayerPrefs.SetInt("gameWidth", gameWidth);
			PlayerPrefs.SetInt("gameHeight", gameHeight);
			PlayerPrefs.SetFloat("gameVolume", gameVolume);
		}
	}
	
	function	saveGameConfig() {
		PlayerPrefs.SetInt("gameWidth", gameWidth);
		PlayerPrefs.SetInt("gameHeight", gameHeight);
		PlayerPrefs.SetFloat("gameVolume", gameVolume);
	}
}

where is my problem ? ( not in the kitchen ^^ )

Is the GameCFG script attached to the object running your Start() code or is it on another object?

Saying “gameObject.GetComponent(GameCFG)” is saying, “Get the GameCFG script that’s attached to myself.”

I imagine that’s not where it is, so the GetComponent method is getting nothing. Then you try to run “getGameConfig()” on nothing – hence a “NullReferenceException”.

Instead maybe you need something like:

function Start(){
	var currentGameConfig : GameCFG = GameObject.Find("yourGameCfgGameObjectName").GetComponent(GameCFG);
	currentGameConfig.getGameConfig();
}

I Want to save integer numbers (a lot of them) do I use same command?
PlayerPrefs.SetInt

I mean is it the only way to store an information on our computer with script

@FizixMan
I Have two script file MainMenu.js who have the Start function
and GameCFG.js who have the class code

the both are attached to the MainCamera

Can I Use this Class without attach it ?

@Majmun
I think so

No one to help for resolving this error ?

You should be able to. PlayerPrefs is a static class.

MainMenu needs to be attached (so the Start() function is called), but GameCFG can exist as a custom class without being attached.

I have tried this :

var currentGameConfig : GameCFG = gameObject.GetComponent("GameCFG");

put GameCFG in double quotes i Have a new error

And the eternal :

you forgot extends MonoBehaviour in your class GameCFG declaration

If I understand your intent, you do not want GameCFG to be a script attached to an object. If this is the case, then maybe you’d have something like:

import UnityEngine;

public class GameCFG
{
	public static var LastSavedLevel : int;
	public static var PlayerName : string;


	public static function LoadConfig()
	{
		LastSavedLevel = PlayerPrefs.GetInt("LastSavedLevel");
		PlayerName = PlayerPrefs.GetString("PlayerName");
	}

	public static function SaveConfig()
	{
		PlayerPrefs.SetInt("LastSavedLevel", LastSavedLevel);
		PlayerPrefs.SetString("PlayerName", PlayerName);
	}
}

To load your config, in your MainMenu.js:

function Start()
{
	GameCFG.LoadConfig();
}

Then in your other scripts, you’d work with it like:

//reading data
var levelToLoad : int = GameCFG.LastSavedLevel;
var playerName : string = GameCFG.PlayerName;


//writing data
GameCFG.PlayerName = "it's a new name!";

//saving data
GameCFG.SaveConfig();

Hi here is same problem, can you solve it…
My class code is


Please reply thanks…