Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 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 /
  • Help Room /
avatar image
1
Question by jxng1 · Aug 15, 2019 at 07:02 PM · scene-loadinggameobject.findscene change

How to access a GameObject in another scene?

I'm very new to Unity and I've encountered a problem. I've also searched online the internet but I can't find anything to help me. What I've tried already is loading the scene I want, then find the GameObject, like so:

    public void LoadDioxygen()
 {
     SceneManager.LoadScene("Simulation", LoadSceneMode.Single);
     GameObject.Find("Dioxygen").SetActive(false);
         
     if (Input.GetKey(KeyCode.Space)) {
         GameObject.Find("Dioxygen").SetActive(true);
         Debug.Log("Active!");
     }       
 }

However, this isn't working. What I've seen online is that I need to keep the Script Controller loaded but I really don't understand how that works. I saw DontDestroyOnLoad() being talked about but I really have no idea where to go at this point. What I'm trying to do is I have a menuscene and a simulationscene. What happens is when you click on a button you go to the simulationscene and it loads the GameObject I want it to load, and there are multiple buttons that loads different GameObjects. Also on the simulationscene there is a Menu button that when pressed, hides the current GameObject in the simulationscene and goes back to the menuscene.

Thank you all for your time! Please do tell me if you need more information.

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 jxng1 · Aug 19, 2019 at 01:19 PM 0
Share

Any help? BU$$anonymous$$P

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by jxng1 · Aug 16, 2019 at 11:11 AM

@Vega4Life

I had already tried the DontDestroyOnLoad but after testing it out for a bit, it appears that:

  public void LoadDioxygen()
 {
     SceneManager.LoadScene("Simulation", LoadSceneMode.Single);/////at this line
     GameObject.Find("Dioxygen").SetActive(false);
     if (Input.GetKey(KeyCode.Space)) {
         GameObject.Find("Dioxygen").SetActive(true);
     }
 }

at that line I get an error of NullReferenceException : Object reference not set to an instance of an object.

Perhaps this is what's causing everything to not work?

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 jxng1 · Aug 16, 2019 at 11:17 AM 0
Share

What I believe is happening is that it loads said the simulation scene but it's still trying to find the gameobject of Dioxygen in the current menuscene, which is why it is unable to find the gameobject as that gameobject doesn't exist in the menuscene? Would that be a correct assumption?

avatar image
0

Answer by Vega4Life · Aug 15, 2019 at 07:14 PM

I would first try to add DontDestroy in your awake function of the above script. So add this:


     private void Awake()
     {
         DontDestroyOnLoad(this);
     }


Then the script sticks around and you should be able to find the game object. Although, it may not find it the first time... since you are loading the scene and immediately trying to find an object that may not be loaded yet.

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

182 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 avatar image avatar image avatar image

Related Questions

Loading and Unloading scenes 2 Answers

Scene Manager and Keeping Objects when Loading/Unloading + Gameogject.SetActive not working:( 0 Answers

Unable to get name of current scene 0 Answers

Save and load scene from file 2 Answers

SceneManager.UnloadSceneAsync followed by LoadSceneAsync, loads the scene twice 0 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