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 /
  • Help Room /
avatar image
0
Question by samccloy4 · Dec 20, 2021 at 12:32 AM · scene-switchingreferencing

keep reference of these game objects between scenes without using prefabs?

when using prefabs in the game object slots I do not get a null error but it doesn't keep track of it properly if I let the script find the game object it works but when I switch scenes I get a null reference.` this script is on a game manager that doesn't get destroyed between scenes

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class doorPanel : MonoBehaviour
 {
    public GameObject doorPanel_0;
     public GameObject doorPanel_1;
     public bool unlocked, unlocked1;
     public bool locked, locked1;
      
 
 
     void Start()
     {
          
 
 
      
     }
 
     // Update is called once per frame
     void Update()
     {
           if (doorPanel_0 == null)
       {
          doorPanel_0 = GameObject.Find("Doorpanel_0");
       }
 
       if (doorPanel_1 == null)
       {
          doorPanel_1 = GameObject.Find("Doorpanel_1");
       }
 
 
 
 
          DoorClosed door0 = doorPanel_0.GetComponent<DoorClosed>();
         DoorClosed door1 = doorPanel_1.GetComponent<DoorClosed>(); 
 
 
           if (door0.doorclosed == true)
         {
     
 
             
               locked = true ;
               unlocked = false;
              
 
 
         }
         if (door0.doorclosed == false)
         {
 
             locked = false;
             unlocked = true;
         }
 
 
    
       
 
     }
 }








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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by CodesCove · Dec 21, 2021 at 06:12 PM

GameObjects (and all scene objects) exists only in their own scene, not anywhere else. Also the reference to these objects are non-presistent because they need to be instantiated again when ever the scene is loaded (or reloaded). Basically they will have new memory addresses and the previous reference to them will not work (that's why you get null reference error).

So only way you can address them in a persistent way is to find them somehow when the scene is loaded.

Now there are multiple ways to do it. The best way (in my opinion) is to use scriptable objects. https://learn.unity.com/tutorial/introduction-to-scriptable-objects

https://answers.unity.com/questions/1875967/finding-objects-on-scene-change.html?childToView=1875980#answer-1875980

This way you can, in addition to have the reference "persistent", also store the object state and other "item properties" like door closed state.

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

175 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

Related Questions

Load Level Script Issue 1 Answer

The scrollrect viewport breaks when changing scene 3 Answers

Switch from Animated camera to FPS camera? (C#) 0 Answers

2D RPG Game Management - How Do I Approach It? 0 Answers

Editor: detect destroy object event when unloading scene 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