- Home /
Accessing Functions from other Scripts
I am trying to make a saving and loading system, I'm using a currentLevel variable's value to determine the last-saved level. I'm been trying for hours to get it to work and I can't, here are my scripts.
(New Level Script)
var endOfLevel : Collider;
function OnTriggerEnter (other : Collider) {
currentLevel = 1;
Application.LoadLevel ("Level2");
}
(Save Script)
*After hitting a collider in Level1, I want the currentLevel be set to 1.*
var currentLevel:int;
function Start () {
currentLevel = 0;
}
function LoadSavedLevel () {
if(currentLevel == 0)
{
Application.LoadLevel("Level1");
}
if(currentLevel == 1)
{
Application.LoadLevel("Level2");
}
}
The game will start with currentLevel = 0, which will mean that the game will load the first level once campaign is clicked. However after the first level is "beat" they will go onto the second level along with currentLevel = 1.
(Menu Script)
var buttonWidth:int = 200;
var buttonHeight:int = 50; var spacing:int = 50; var menuSkins :GUISkin; var rolloverSound : AudioClip;
function Start(){ gameObject.GetComponent (TextBox).enabled = false; }
function OnCollisionEnter () { audio.PlayOneShot(rolloverSound); }
function OnGUI() {
GUI.skin = menuSkins;
GUILayout.BeginArea(Rect(Screen.width/8 - buttonWidth/2, Screen.height/1.4 - 200, buttonWidth, 400));
if(GUILayout.Button("CAMPAIGN", GUILayout.Height(buttonHeight)))
{
LoadSavedLevel();
}
GUILayout.Space(spacing);
if(GUILayout.Button("QUICK MATCH", GUILayout.Height(buttonHeight)))
{
//Nothing Yet
}
GUILayout.Space(spacing);
if(GUILayout.Button("PROFILE", GUILayout.Height(buttonHeight)))
{
gameObject.GetComponent (TextBox).enabled = true;
gameObject.GetComponent (TitleGUI).enabled = false;
}
GUILayout.Space(spacing);
if(GUILayout.Button("OPTIONS", GUILayout.Height(buttonHeight)))
{
gameObject.GetComponent (OptionsMenu).enabled = true;
gameObject.GetComponent (TitleGUI).enabled = false;
}
GUILayout.Space(spacing);
if(GUILayout.Button("EXIT", GUILayout.Height(buttonHeight)))
{
Application.Quit();
}
GUILayout.EndArea();
}
Once the Campaign button is clicked I want it to load the currentLevel = 2 from the save script.
Hopefully the way I explained wasn't too confusing, any help would be appreciated.
So you want the last level played to be saved for later uses? I have not tried it but I would think of printing it out on a .txt file or maybe X$$anonymous$$L that the program fetches at start. Have a go there maybe http://answers.unity3d.com/questions/141782/how-do-i-read-read-from-and-write-to-a-text-file.html
using xml is a little bit much for what is needed here and using PlayerPrefs would be a better choice.
@JDPennock would you have any links to some reference scripts to explain all that in details?
Answer by JDPennock · Mar 15, 2012 at 07:31 AM
Ok I don't have a lot of time left here to give a great answer, but basically you will want to look into using PlayerPrefs to save and load what level has been completed.
And you will want to look into using GetComponent to access your functions written in different scripts.
Sorry this is so short. Keep trying and I'll check back tomorrow to see if you got it sorted out.
Well I tried this and it only loads the second level, unless I remove its line of code.
function LoadSavedLevel () {
var test : int = PlayerPrefs.GetInt("CurrentLevel", 1);
Application.LoadLevel("Level1");
var test1 : int = PlayerPrefs.GetInt("CurrentLevel", 2);
Application.LoadLevel("Level2");
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Save Player Health and stats 1 Answer
error CS1023: An embedded statement may not be declaration or labeled statement 1 Answer
Saving/Loading Problems 1 Answer
Saving and Loading 0 Answers