• 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 Alex2k17 · Jun 28, 2017 at 05:03 PM · scenesdropdownbetween

Change language to all scenes

I want to change and save the language of the game from "Options" scene to all scenes. I can change and save the language only in "Options" scene from a dropdown list, but when I try to set the text from another scene i get this error: "Object reference not set to an instance of object". Script for "Options":

 void Start()
     {
         if (PlayerPrefs.HasKey ("dropdown")) {
             dropdown.value = PlayerPrefs.GetInt ("dropdown");
         }
     }
 
     void Update () {
         switch(dropdown.value) 
         {
         case 0:
             snd.text = "Volum sunet";
             msc.text = "Volum muzica";
             lng.text = "Limba";
             menu.play.text="Joaca";
         break;
         case 1:
             snd.text = "Sound volume";
             msc.text = "Music volume";
             lng.text = "Language";
             menu.play.text="Play";
             break;
     }
         PlayerPrefs.SetInt ("dropdown", dropdown.value);
 }

Script for "Menu" scene

 void Start()
     {
         if (PlayerPrefs.HasKey ("dropdown")) {
             lang.dropdown.value=PlayerPrefs.SetInt("dropdown");
         }
     }

I don't really know to access dropdown value from another scene.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by yoHasse · Jun 29, 2017 at 09:09 AM

"Object reference not set to an instance of object". Script for "Options"

You get this error because when loading the next scene the Option object in the last scene is destroyed. Therefore when you try to access it you get the error that the object doesn't exist. What you can do is to use "DontDestroyOnLoad(Object target);". This should keep the object from getting destroyed and then you should be able to reference it from the other scenes

Reference:

https://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html

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 Alex2k17 · Jun 29, 2017 at 10:46 AM

If I use DontDestroyOnLoad(Object target); on Script 1, automatically can I use reference to Script 2? Please note that I want to modify text, not objects.

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 yoHasse · Jun 29, 2017 at 12:34 PM 0
Share

Well no, "lang" needs to be referenced somewhere first in the Start method. Below are two different examples where "Option" is the name of your script.

 lang = GetComponent<Option>(); //if the script is attached to the same gameobject
         
 lang = FindObjectOfType<Option>().GetComponent<Option>();
         
 //FindObjectOfType<Option>() finds the first gameobject with option component attached
 //Gets the Option script attached to that object.

Referencing to other scripts is a really handy skill to have, I suggest that you look at some tutorials on Unitys website to learn some more.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

67 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

Related Questions

Passing "best" to the other scene 0 Answers

Create a scene with the information selected in the main menu 1 Answer

Application.LoadLevel loading the current scene instead of the specifed one 1 Answer

time between scenes (loading time) 0 Answers

Transfer data between scenes + saving it C# 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