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
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();
}
HrC123
February 3, 2011, 5:50pm
8
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
//now create a class
class MSteeringOutput
{
var linear:Vector2;
var angular:float;
}
class MLocation extends MSteeringOutput
{
var position:Vector3;
var orientation:float;
function MLocation()
{
orientation=0.0f;
}
function MLocation(other:MLocation)
{
position=other.position;
orientation=other.orientation;
}
function MLocation(lposition:Vector3)
{
position=lposition;
orientation=0.0f;
}
function MLocation(lposition:Vector3,lorientation:float)
{
position=lposition;
orientation=lorientation;
}
function MLocation(x:float,y:float,z:float,lorientation:float)
{
position.x=x;
position.y=y;
position.z=z;
orientation=lorientation;
}
function clear()
{
position.x=0.0f;
position.y=0.0f;
position.z=0.0f;
orientation=0.0f;
}
function integrate(steer:MSteeringOutput,duration:float)
{
}
function getOrientation():Vector3
{
}
}
class MKinematic extends MLocation
{
var velocity:Vector3;
var rotation:float;
function MKinematic()
{
Debug.Log(“SUPER CLEAR METHOD”);
** MLocation();**
** velocity.x=0.0f;**
** velocity.y=0.0f;**
** rotation=0.0f;**
}
function MKinematic(lposition:Vector3,lvelocity:Vector3)
{
MLocation(position);
velocity=lvelocity;
rotation=0.0f;
}
function MKinematic(loc:MLocation)
{
MLocation(loc);
velocity.x=velocity.y=velocity.z=0.0f;
rotation=0.0f;
}
function MKinematic(loc:MLocation, lvelocity:Vector3)
{
MLocation(loc);
velocity=lvelocity;
rotation=0.0f;
}
function MKinematic(lposition:Vector3,lorientation:float,
lvelocity:Vector3, avel:float)
{
MLocation(lposition, lorientation);
velocity=lvelocity;
rotation=avel;
}
function clear()
**{ **
this.rotation = 0.0f;
}
}
function Update () {
var x:MKinematic=new MKinematic();
}
Please reply thanks…