• 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
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
avatar image
0

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?

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

How to passing levels automatically in games? 0 Answers

Lifes count resets with level 0 Answers

How to change transform.position after loadScene? 2 Answers

Camera not rendering on UnloadSceneAsynch 0 Answers

Printing total time to open/loading a scene 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges