Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 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 QuestionBro · Nov 30, 2010 at 11:32 PM · javascriptscenefunction

How to call a function from a script in another scene

is there a way to call a function on a script in another scene? or effect a script in another scene being active or not? I am trying to create difficulty settings and I would like to have my title screen on its own scene and make it call a function or a script in the next scene to set whichever difficulty the player has chosen, I am simply unsure of how to go about linking the menu script to the script in the main game.

Is there a way to do this? Or should I just make my title screen in my main game? I am currently using javascript

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

5 Replies

· Add your reply
  • Sort: 
avatar image
4

Answer by syamilsynz · Jun 30, 2015 at 11:07 AM

In scene 1.

Create empty GameObject and name it "Script". Attached a script (eg: class name "Scene1.cs") that want to be calls from other scenes. Apply DontDestroyOnLoad on it.

 void Awake()
 {
   DontDestroyOnLoad(this);
 }

 public void TestCall()
 {
 // test
 }

In scene 2.

 private Scene1 scene1;
     // Use this for initialization
     void Start () 
     {
         scene1 = GameObject.Find("Script").GetComponent<Scene1>();
         scene1.TestCall();
     }





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
3

Answer by Peter G · Nov 30, 2010 at 11:42 PM

There are several ways to do this.

  1. You could make the difficulty a class variable (static keyword) then have each scene check the difficulty variable on this script. This would be the opposite of your idea, but would be just as easy, if not easier.

  2. You could use DontDestroyOnLoad() on some script in your opening scene so that the difficulty manager is not destroyed when the new scene loads.

  3. I am not sure how skilled of a programmer you are, but you could create a singleton of you individual scene managers, and then change the difficulty there.

  4. You could use PlayerPrefs to store a value to load in the next scene. This method is not really ideal, and I would only use it if the other 3 fail.

The problem is that instances of your scripts are created and destroyed when a scene loads, not when the game loads, so you either have to create a class field or singleton or use DontDestroyOnLoad.

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 I9ball · Aug 29, 2012 at 04:19 PM 0
Share

So by the solutions you listed, would a viable solution be keeping one object as a scene loader (which also holds globals) and loading scenes with the loadLevelAdditive() method?

avatar image CoolCosmos · Aug 13, 2020 at 11:04 AM 0
Share

@Peter G Why don'T you suggest PlayerPrefs method ? Why should i avoid to use it ?

avatar image
0

Answer by moment_um · Oct 08, 2018 at 04:15 AM

There are 2 straight forward answers that I can think of.

  1. Make all members in the class static. Making them static is super easy and convenient, but there are a couple problems that you run into: 1, you can't have more than 1 of a member, because static members belong to the Class and not the instance of a class. 2, Unity does not support editor drag and drop functionality of static members ([serilizefield] or public functionality). There are probably some fancy things you could do to get around that, but it's not straight forward. If you must have this,

  2. Make a Singleton helper. a class that calls the singletons methods. The most standard way to make a Singleton is to make a public static instance of itself like this public static MenuManager instance; void Awake() { if (instance) { Destroy(gameObject); } else { instance = this; } } then in another class call the singletons methods using that instance

    public void DoSomething(){ SingltonClass.instance.DoSomething(); } you can put that class in all your scenes, but you only need to put the singleton in your main menu scene, or whatever scene you wish.

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 catch_up · Feb 23, 2020 at 11:33 AM

I've loadded Settings scene from MainMenu scene, and now both of the scene exists

 SceneManager.LoadScene("Settings", LoadSceneMode.Additive);



I've a game object in MainMenu scene on which MainMenuController monobehavior is attached. Now, in SettingsController.css, I can call a function inside MainMenuController.cs

 function void SomeFunc() 
 {
     MainMenuController mainMenuController = GameObject.Find("MainMenuController").GetComponent<MainMenuController>();
     
     mainMenuController.DoSomething();
 }
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 CoolCosmos · Aug 13, 2020 at 11:07 AM

Peter says that he doesn't suggest PlayerPrefs method to use. Can anyone tell me why i should avoid to use this method ?

I think, it would be easier to save variables and then call it on anywhere you like... I'm not sure though...

Comment
Add comment · Show 1 · 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 danBourquin · Mar 14 at 02:17 PM 0
Share

Because playpref will save the data in the device and theses data will persist between game session. On window it will be save on the window registery. I'm pretty sure it's a bad idea for what you want to do. A singleton pattern (~ using static reference) is better.

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

Creating a scene using script (which Start() to use) 2 Answers

Why is my script still running in other scenes? 1 Answer

How to start a function from another scene. 1 Answer

Accessing function from another script won't work 1 Answer

Function is called twice and I've no idea why 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