Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
This question was closed Nov 01, 2016 at 08:15 PM by Eagle-Bound for the following reason:

Other

avatar image
0
Question by Eagle-Bound · Oct 26, 2016 at 01:27 AM · c#scenestringnoobsimple

PlayerPrefs string to load scene (edited)

How can I uses PlayerPrefs to load a scene? My script looks like this:

 using UnityEngine;
 using System.Collections;
 
 public class LoadandSaveManager : MonoBehaviour {
     
     public string thisScene;
 
 
     public void SaveGame(){
 
 
             PlayerPrefs.SetString (thisScene, "0");
         Debug.Log ("Saved");
 
 
     }
 
     public void LoadGame(){
 
         Application.LoadLevel (PlayerPrefs.GetString (thisScene));
 
     }
         
 }

I feel like its a really simple fix, but I can't figure out how to do it, so if anyone could tell me how to, it would be very helpful.

Thanks!

Comment
Add comment
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

1 Reply

  • Sort: 
avatar image
1

Answer by Cynikal · Oct 26, 2016 at 01:32 AM

 Application.LoadLevel(PlayerPrefs.GetString("WhatScene"));

That SHOULD work... However...

You should also make sure it's not null..

 if (!String.IsNullOrWhiteSpace(PlayerPrefs.GetString("whatScene")))
 {
 Application.LoadLevel(PlayerPrefs.GetString("whatScene"));
 } else {
 Debug.LogError("Something is not right...");
 }
Comment
Add comment · Show 3 · 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 Eagle-Bound · Oct 26, 2016 at 01:46 AM 0
Share

Thank you so much for your quick response. I have a couple questions, though. Right now I have two scripts, one for the load game and one for the save game. Is that how I should go about it? Or is there a better way? Second, my plan was to have whatScene to be a variable so I would only need one script and I could use it throughout all my game. Is this possible? If so, how would I do it? Once again, thank you so much, and I hope I'm not being too confusing.

avatar image Cynikal ♦ Eagle-Bound · Oct 26, 2016 at 01:50 AM 0
Share

Simple answer: Yes.

There is almost never a difference in breaking stuff up into smaller files, or implementing it all in one file. So, you can have the save stuff in one file, and the load stuff in another.

Since "whatScene" is a variable, you could write directly to the playerprefs and pull it at will, or have a gamemanager script to pull the info from it as well.

I can't tell you if it's the right way of going about it, since I literally know nothing about your game, other than you're wanting to load a level based off of a variable.

Do what works. If it works.. Don't question it, until you start seeing reasons to.

avatar image Eagle-Bound Cynikal ♦ · Oct 26, 2016 at 07:55 PM 0
Share

I know I'm being a bit annoying, and I apologize for that, but I can't seem to be able to check if one button is pressed from a script. I know you can use OnButtonUp , but I'm not sure how to check if another button is being pressed without splitting it into another script. Again, sorry for all the questions, but I am fairly new to Unity, so I'm still trying to get a grasp on things.

Follow this Question

Answers Answers and Comments

231 People are following this question.

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

Related Questions

Distribute terrain in zones 3 Answers

Simple networking - Sending string over network? 1 Answer

Replace the first string C# 1 Answer

Text isn't showing up on button 0 Answers

Deactivate game modes until tutorial is finished? 1 Answer


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