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 /
This post is currently awaiting moderation. If you believe this to be in error, contact a system administrator.
This question was closed Dec 13, 2020 at 05:45 PM by Hellium for the following reason:

Duplicate Question

avatar image
0
Question by Dracolyte · Dec 13, 2020 at 05:28 PM · scenenullreferenceexceptionscene-loadingreferencescene-change

How do I reference something from another scene?

I searched everywhere for anwers and haven't found answers. I have a MainMenu script and there is a reference that becomes a null reference if i switch to my playscene. That MainMenu script is attached to a MainMenu gameobject (which is in the MainMenu scene) and to a button in the playscene, so I can use the methods inside. The problem is, that because its in the playscene too, it cant reference a text because it doesn't exist in the playscene. I tried things like "DontDestroyOnLoad" but it doesn't work. I need to somehow reference it within playscene, but so that the text is still in the MainMenu scene. I dont know how to describe this. If you know an easier way to get rid of this NullReferenceExecption, then please tell me. I literally found nothing while digging in the internet.

The error: NullReferenceException: Object reference not set to an instance of an object MainMenu.Start () (at Assets/Scripts/MainMenu.cs:17)

 private void Start()
 {
     _playerStats = FindObjectOfType<PlayerStats>().GetComponent<PlayerStats>();
     _allMoneyText = FindObjectOfType<AllMoneyText1>().GetComponent<AllMoneyText1>();
     _allMoneyText.ChangeMoneyText();
 }

 public void PlayGame()
 {
     SceneManager.LoadScene("Game");
 }

 public void BackToMainMenu()
 {
     _playerStats.AllMoney += _playerStats.money;
     SceneManager.LoadScene("MainMenu");
     SaveSystem.SavePlayer(_playerStats);
     _allMoneyText.ChangeMoneyText();
     Debug.Log(_playerStats.money);
     Debug.Log(_playerStats.AllMoney);
 }

 public void QuitGame()
 {
     SaveSystem.SavePlayer(_playerStats);
     Application.Quit();
 }

}

Comment
Add comment · Show 1
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 Hellium · Dec 13, 2020 at 05:44 PM 0
Share

You can't reference a gameObject present in a non loaded scene.

Either load that scene in additive, or save the data you want in your 1st scene and load that value in the 2nd scene.

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

179 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

Related Questions

Using UI.Button to Load Another Scene? (using outdated tutorial video) 1 Answer

Scene Loading Issue. 0 Answers

Unity Scene loading problem (slow loading until it freezes) 0 Answers

Scene loading delay after upgrade 4.6 to 5. 0 Answers

How Do I Link Different Scenes? 4 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