Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 breadtop · Jan 13, 2017 at 11:47 AM · scripting problemuimenu screen

How to link buttons to individual levels within the same scene?

Hi,

I'm using Unity to build a game but I want to add a start screen to it, with buttons for Level 1, Level 2, Level 3 and Level 4. I want the buttons, once pressed on, to go straight to that level and if the player passes it, to continue on with the next randomly generated levels. (I'm basing the game off the Unity 2D Roguelike Tutorial)

 using UnityEngine;
 using System.Collections;
 using UnityEngine.SceneManagement; // Allows SceneManager to be used.
  
 public class LoadSceneOnClick : MonoBehaviour
 {
  
     public void LoadByIndex(int sceneIndex) // Passes in what scene will be loaded.
     {
         SceneManager.LoadScene(sceneIndex); // SceneManager loads scene.
     }
 }

This is my code for my LoadSceneOnClick script that I am using for each of my main menu buttons. I got this from the Creating A Main Menu tutorial. However it is not working because it assumes that each level is a different scene. This game contains all its levels in a single scene so I'm not sure how to make this work. I've tried passing in the variable for level instead of sceneIndex but it did not work, so maybe I was not doing it right.

Any help would be appreciated!!

Thanks :)

Comment
Add comment · Show 3
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 LK84 · Jan 13, 2017 at 12:06 PM 0
Share

what kind of varable do you want to pass? (I'ver never done the Roguelike Tutorial). Is it some position in world space where your player is supposed to be?

avatar image breadtop LK84 · Jan 13, 2017 at 12:53 PM 0
Share

I want to pass a variable that stores an integer value that is what level I want the game to start at (like how I'm doing it in the picture).alt text

level-button.png (31.3 kB)
avatar image LK84 breadtop · Jan 13, 2017 at 01:05 PM 0
Share

You can look into scriptable Objects ( https://unity3d.com/de/learn/tutorials/modules/beginner/live-training-archive/scriptable-objects ) or create a game $$anonymous$$anager based on the singleton pattern witho DondtDestroyOnLoad (http://wiki.unity3d.com/index.php/Singleton ) . I personally prefer the first option.

1 Reply

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

Answer by jwulf · Jan 13, 2017 at 12:29 PM

I don't know what the Roguelike Tutorial is, but if you have multiple Levels in one Scene, you could achieve what you want to do like:

 public void LoadByIndex(int levelIndex) 
 {
     PlayerPrefs.SetInt("initialLevel", levelIndex); // Store which level in the loaded scene will be played
     SceneManager.LoadScene("yourSceneName"); // SceneManager loads scene.
 }

And then in your scene with the 4 levels, have some sort of LevelController, which (e.g. in the Start()-Method) does something like:

 int i = PlayerPrefs.GetInt("initialLevel");
 // TODO start the level identified by i --> Set Player's initial position to level i's position, spawn enemies in level i, etc...
Comment
Add comment · Show 4 · 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 LK84 · Jan 13, 2017 at 12:36 PM 0
Share

PlayerPrefs should never be used for important data such like level progress (as far as I understand from breadtop description a new level gets unlocked if the player passes the previous one and you don't want the player to manipulate it that easy)

avatar image jwulf LK84 · Jan 13, 2017 at 12:40 PM 0
Share

True, but I thought, the 4 levels are all supposed to be available from the beginning and when one is passed, a randomly generated level is started.

If it should be safe from manipulation, storing a serialized class in the PersistentDataPath would be better.

avatar image breadtop · Jan 29, 2017 at 07:37 AM 0
Share

Thanks, this worked really well!

avatar image jwulf breadtop · Jan 29, 2017 at 04:18 PM 0
Share

Good to know! :)

Please take L$$anonymous$$84's objection into account that using the PlayerPrefs is not safe from manipulation by a user, if that's a problem you want to consider in your use case.

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

Check the toggle status of other game objects from a script 0 Answers

How to send mouse events directly to the canvas in world pos 0 Answers

Basic question about creating a menu 1 Answer

Scene Reload/Respawn system destroys UI 2 Answers

Sprite shrinks when selected? 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