Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Chimera3D · Mar 15, 2012 at 06:57 AM · loadingsavingfunctions

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.

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image fafase · Mar 15, 2012 at 07:22 AM 0
Share

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

avatar image JDPennock · Mar 15, 2012 at 07:26 AM 0
Share

using xml is a little bit much for what is needed here and using PlayerPrefs would be a better choice.

avatar image fafase · Mar 15, 2012 at 07:32 AM 0
Share

@JDPennock would you have any links to some reference scripts to explain all that in details?

avatar image JDPennock · Mar 15, 2012 at 07:33 AM 0
Share

I put them in the answer I just gave.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image JDPennock · Mar 15, 2012 at 08:05 PM 0
Share

Did you get it working?

avatar image Chimera3D · Mar 18, 2012 at 07:36 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges