• 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 Afelium · Feb 15, 2019 at 10:34 PM · prefabdestroyprefabsscene-loadingscene-switching

References to Prefab get lost on start

I'm following a tutorial for a top-down RPG and in the beginning of this episode

he puts the player prefab(and the Canvas(stats) and Camera)in another scene so that,even when not starting from the main scene,there's always one of these objects(so that testing is easier).

When he does that there isn't any issue,but when I try it all the references to the Player and the other prefabs become null whenever the player moves from a scene to another.


This code is in the start function of the objects mantained between scenes

 private static bool playerExists;//becomes true whenever a player object is created if there isn't another one in the current scene
 
 
 if (!playerExists)
         {
             DontDestroyOnLoad (transform.gameObject);
             playerExists = true;
         } 
         else 
         {
             Destroy (gameObject);
         }

This code should destroy the prefabs in the current scene if ones from others are already present(this way there will always be one player,camera and stats per scene)


I think that this happens because in the start functions of objects local to the scene, the object references are set with FindObjectOfType() to the prefabs local to the scene(local player,camera and stats),but then they are destroyed and the new prefabs coming from another scene take their place,but with missing references.


VVV UPDATE:tested this hypotesis,put the result in reply VVV


What I don't understand is this doesn't happen in the tutorial,and before trying any wacky workaround I'd like to know why if you need more specific info/code ask and I'll post it ASAP

Comment
Add comment · Show 13
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 xxmariofer · Feb 15, 2019 at 11:02 PM 0
Share

has he set up any fields in the inspector that you are missing? the new scene has all the prefabs that you are trying to search when loading the other scene? can you tell us what vars are the ones giving you issues? when starting from the second scene the game doesnt work?

Show more comments
avatar image zereda-games · Feb 16, 2019 at 02:55 AM 0
Share
 private static Flexible$$anonymous$$usic$$anonymous$$anager _instance;
 
 public static Flexible$$anonymous$$usic$$anonymous$$anager instance {
     get {
         if (_instance == null) {//in case not awake yet
             _instance = FindObjectOfType<Flexible$$anonymous$$usic$$anonymous$$anager> ();
         }
         return _instance;
     }
 }
 
 void Awake ()
 {
     // if the singleton hasn't been initialized yet
     if (dontDestroyOnLoad) {
         if (_instance != null && _instance != this) {
             Debug.LogError ("Duplicate singleton " + this.gameObject + " created; destroying it now");
             Destroy (this.gameObject);
         }
 
         if (_instance != this) {
             _instance = this;
             DontDestroyOnLoad (this.gameObject);
         }
     } else {
         _instance = this;
     }
 }
Show more comments
avatar image Afelium · Feb 19, 2019 at 09:33 PM 0
Share

UPDATE:


confirmed my hypotesis by using debug.Log with startpoint's thePlayer attribute value + name of the current scene,both at start and update: the reference wasn't null in the Start function's log,while in the update it was. Tried to change the execution order,putting the PlayerController script(which includes the method used above) before all the others but the references to the player were still null. Does the script execution order even affect the start functions?Are they executed in another order?


Tried to fix by putting the method above inside the awake(),but it seems like that dontDestroyOnLoad() does not work now,the player is recreated in each scene

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Afelium · Mar 01, 2019 at 04:04 PM

I finally found a solution! I discovered that the Destroy() method isn't very reliable, and for some reason the references were set to the superflous prefab before its destruction. I fixed this by using the DestroyImmediate() method and now everything works fine(but I read its use is not advised).

(I still put the singleton method inside of Awake() )


Still don't understand why the guy from the tutorial didn't have to use it.

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.

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

200 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 avatar image avatar image avatar image avatar image avatar image avatar image 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 load scenes so that my location is saved 0 Answers

How can I destroy a prefab clone? 1 Answer

(VR) Switch Player Prefabs on Load Scene 1 Answer

Pre-loading scene and display it only when I want 0 Answers

Ball game -ball stuck,Ball stuck after switching scenes. 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