• 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
Question by daringly · Apr 26, 2012 at 03:04 AM · loadlevelreload

Function broken after Application.LoadLevel()

So I have a game object with the following script attached to it:

public class Enemy : MonoBehaviour {

 public void MoveTo( Vector3 pos ){

   this.gameObject.transform.position = pos;
 }

}

It works great, no problem until I use: Application.LoadLevel("LevelName"). After the level reloads and I try to use the "MoveTo" function, I get this error:

"MissingReferenceException: The object of type 'Enemy' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object." To confirm I've striped everything out of the level so there is only a single "Enemy" object.

It's my understanding that "LoadLevel" should dump everything from memory (except things that are static of course). So I would expect the game object with the "Enemy" script attached to it to be removed from memory and then reloaded and work fine. It seems somehow the "this.gameObject" is still referencing the older game object that was deleted and not the new one that has been loaded. I don't understand why "this.gameObject" doesn't evaluate to the new game object. I must be missing something. Any help would be much appreciated!

Cheers!

Comment

People who like this

0 Show 2
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 syclamoth · Apr 26, 2012 at 03:13 AM 0
Share

Where are you calling 'MoveTo' from?

avatar image daringly · Apr 26, 2012 at 06:10 AM 0
Share

Sorry, I should have put more info. MoveTo() was being called from another function inside the Enemy class. So it should be the game object that has the enemy class attached to it. It works as expected when the level loads the first time, but comes up with the error about the LoadLevel(). Thanks for your help.

2 Replies

· Add your reply
  • Sort: 
avatar image

Answer by pudd1nG · Apr 26, 2012 at 03:19 AM

You're not calling the function from inside anything. I would either call it dynamically from your load function, or simply assign it under the Start() function.

Comment

People who like this

0 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 daringly · Apr 26, 2012 at 06:17 AM 0
Share

I had tried declaring 'gameObject enemy' outside of any functions, but within the Enemy class and then assigning it in Start() like this: 'enemy = this.gameObject' however it gave the same error after a LoadLevel(). Any other ideas? Thank you.

avatar image

Answer by daringly · Apr 26, 2012 at 10:46 PM

I've done some more research and I think this problem might have to do with me using a singleton (a static class). I've read other people have had problems with Start() not running on objects when using a static var. Does anyone have experience using Application.LoadLevel() while using a singleton? Thank you.

Comment

People who like this

0 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 daringly · Apr 26, 2012 at 10:59 PM 0
Share

Also forgot to mention, that I am manually assigning the static vars in the singleton again after the reload.

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

If Statement Conditional Not Working 1 Answer

Application.Loadlevel doesn't work even though my level is in Build Settings 2 Answers

Application.LoadLevel problem. 1 Answer

cardboard gazed at to load new scene 0 Answers

My scene won't load after building the project but i can hear the audio 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