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
11
Question by fanling3 · Mar 20, 2012 at 06:56 AM · parameter

Passing parameters between scences

For example I have 2 scenes named "Game" and "Ending", and after the scene "Game" finished, I will load the scene "Ending" by

 Application.LoadLevel("Ending");

But what if I have 2 endings for example inside "Ending", I have something like

 if (ending == 0) {
    LoadEnding1();
 } else {
    LoadEnding2();
 }
   

so that I want to pass the variable "ending" from scene "Game" to scene "Ending"? 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

3 Replies

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

Answer by Ches81 · Jan 24, 2013 at 02:36 PM

Just stumbled across this thread. I would recommend a class named ApplicationModel in which you insert a static variable named "ending" or something like that. Static variables stay even if you change scenes.

You can access the attribute like this: ApplicationModel.ending

Example class with static attribute:

 public class ApplicationModel  
 {
    static public int ending = 0;    // this is reachable from everywhere
 }

 

Example usage:

 if (ApplicationModel.ending == 1)
 {
    LoadEnding1();
 
 } else 
 {
    LoadEnding2();
 }



EDIT: I published a blog post on this topic today covering both methods that where mentioned here. Including an example project for download.

You can check it out here: click me


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 pointcache · Aug 03, 2014 at 01:43 PM 0
Share

this ^^^^^^^^^^^^^ is gold

avatar image dtumolo · Oct 10, 2018 at 08:25 PM 0
Share

A static variable can't be changed though. I suspect what he wants to do is change which ending is shown based on the actions of the player.

In this case, static won't work.

avatar image Ches81 · Oct 11, 2018 at 05:31 PM 1
Share

I think you confused static with const, which indeed you cannot change. :)

avatar image
7

Answer by syclamoth · Mar 20, 2012 at 07:05 AM

The simplest way to do that, would be to have an empty object that contains that 'ending' variable, and use

 DontDestroyOnLoad(endingObject);

before you load the next scene. Then, you can find (however you choose to do so) the ending object, and extract the variable from that!

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

Answer by tubelightboy · Jun 16, 2021 at 08:42 PM

Just like how @Ches81 pointed out you can use a static variable and access it every time. This is more secure.

Another way is by using PlayerPrefs. using PlayerPrefs, you can set values to variables and check those whenever you want. This is the easiest in my opinion, but might not be secure (unless you decide to use some kind of encryption techniques to encrypt the messages in playerprefs).

In your Game Script, use this when the level ends

 PlayerPrefs.SetInt("Ending", 0);

In your Ending Script use this

int Ending = PlayerPrefs.GetInt("Ending");

 if(Ending == 0){
       //do what you want
 }
 
 else{
       //alternate ending
 }

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

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

8 People are following this question.

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

Related Questions

Passing a Script Name to a Function 2 Answers

Blend tree like effect on one animation ? 0 Answers

How to pass Predicate to a Coroutine? 1 Answer

RPC , string parameter is sent, but length is 0 1 Answer

Instanciate with parameter 2 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