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 /
avatar image
0
Question by Banister88 · Jun 06, 2012 at 03:14 PM · errorgameobjectsceneloadleveldeleted

Can't use variable after scene reload

My current project is set up such that I have 3 scenes, a main menu scene, a scene where the user plays the game, and then a highscore scene. I have it so that from the highscore screen you can click back to the main menu and it will load that scene. The problem is that if I try and use the buttons on the main menu I get a "GameObject has been deleted" missing reference error. I've narrowed it down to the fact that in my menu (I'm using EZ GUI, although it doesn't appear to be ez gui because the messages are getting through) I SetActiveRecursively(false) the panels that I am not using and SetActiveRecursively(true) the one that I am using. So if the user clicks on the instructions button all of the other panels except the instructions panel get deactivated and the instructions panel gets activated.

The exact error occurs on one of those lines, a mainMenuPanel_.SAR(true); line. I thought maybe initially it was because my public GameObject references had lost their reference, but the problem persists even if I load in those references on Awake or Start through code. Has anyone seen anything like this before? If I make a new project and have 2 scenes that link back and forth to each other with buttons, I don't seem to have this problem, so it must be something subtle that I'm doing.

Any thoughts? Thanks in advance for any responses.

EDIT: If I debug out the references in awake and start they appear to be fine, but the reference is null in other functions in that same script even though it's using the same reference as start and awake. Even if I try and reinitialize the variable in this function (just to see if it works) it errors. It's like the reference itself is invalid, even if I try and reassign it.

below is my code:

 using UnityEngine;
 using System.Collections;

 public class MenuController : MonoBehaviour 
 {
     private GameObject mainMenuPanel_;
     private GameObject interfacePanel_;
     private GameObject instructionsPanel_;
 
     private void Awake()
     {
     //this works fine as well, interacting with the panels that I have defined globally above.
     mainMenuPanel_ = transform.FindChild("MainMenu").gameObject;
     interfacePanel_ = transform.FindChild("InterfaceMenu").gameObject;
     instructionsPanel_ = transform.FindChild("InstructionsMenu").gameObject;
     
     MainMenuResponder.interfaceHit += switchToInterface;    
     MainMenuResponder.instructionsHit += switchToInstructions;
     
     InterfaceMenuResponder.backHit += switchToMain;
     InstructionsMenuResponder.backHit += switchToMain;
   }
 
 private void Start()
 {    
     //This works fine, also interacting with the panels.
     switchToMain();
 }
 
 private void switchToInterface()
 {    
     mainMenuPanel_.SetActiveRecursively(false);
     interfacePanel_.SetActiveRecursively(true);
     instructionsPanel_.SetActiveRecursively(false);
 }
 
 private void switchToMain()
 {        
     mainMenuPanel_.SetActiveRecursively(true);
     interfacePanel_.SetActiveRecursively(false);
     instructionsPanel_.SetActiveRecursively(false);
 }
 
 private void switchToInstructions()
 {    
     Debug.Log(mainMenuPanel_); //this is null the second time that I load it, despite it working in Start and Awake
     
     mainMenuPanel_.SetActiveRecursively(false);
     interfacePanel_.SetActiveRecursively(false);
     instructionsPanel_.SetActiveRecursively(true);
 }

}

I have them as private to make sure it wasn't a problem with public references, I originally had them as public and had just dragged the respective GameObjects onto the links in the inspector, but I wanted to make sure that it wasn't the problem. The events that I'm subscribing to in the Awake function are driven by scripts that are children of this GameObject and are in the same scene.

Comment
Add comment · Show 8
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 whydoidoit · Jun 07, 2012 at 12:59 PM 0
Share

You do have DontDestroyOnLoad set on things that need to persist between scenes?

avatar image Banister88 · Jun 07, 2012 at 02:17 PM 0
Share

Yes, but I don't need this to be persisted between scenes. I just need to be able to use the variable and GameObject after I Application.LoadLevel(..). It's not a persistence issue, it's just menu buttons, etc. I don't need them in any other scene.

avatar image whydoidoit · Jun 07, 2012 at 02:19 PM 0
Share

Well if you LoadLevel your objects will be destroyed... Unless they have DontDestroyOnLoad

avatar image Banister88 · Jun 07, 2012 at 02:33 PM 0
Share

I don't need to access any of these objects or scripts outside of their scene, they can be destroyed on load, but when I load the level it is supposed to reload the objects that are currently in that scene, and it doesn't appear that it is. For example, if I make a new project and make 3 scenes with buttons that switch around between the 3 scenes, all of the GameObjects and anything else in those scenes will get reloaded when I load that level and I'm not applying DontDestroyOnLoad to anything. I'm pretty sure DontDestroyOnLoad is in there for the case where I want to persist GameObjects and components across multiple scenes, not the case where I want things to be loaded when I call LoadLevel.

avatar image whydoidoit · Jun 07, 2012 at 02:38 PM 0
Share

Very true. But you get that error if you still have a reference to an object in the previous scene and try to do something with it. In your question you mention calling SetActiveRecursively on something - so you think you have re-established that object to the one that has just been reloaded?

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Deleted GameObject on Scene Load 0 Answers

Keep GameObject to variable linkage on LoadLevel 0 Answers

Change level upon clicking a gameobject 2 Answers

Weird GetComponent error 2 Answers

Quick question. How do i get an object to come down once all AIs are destroyes and then when i come near it it loads a level 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