Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 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?

avatar image Afelium xxmariofer · Feb 19, 2019 at 09:17 PM 0
Share
 has he set up any fields in the inspector that you are missing?

I've checked the scripts involved,and nothing seems missing

  the new scene has all the prefabs that you are trying to search when loading the other scene?

Yes


 can you tell us what vars are the ones giving you issues?

The most noticeable is thePlayer,reference variable in the startPoint (which is an empty object that should set the player's and Camera's transform to its location when the scen is loaded,it finds them thanks to findObjectOfType);


It is located in front of a door.It doesn't know what thePlayer is,so the one co$$anonymous$$g from the other scene spawns out of the map/wrong place,same goes for the camera(less noticeable,as it quickly moves to the player with lerp).


There's a similar issue with the questTriggers(they activate quests when the player walks on them;The quest manager is a child of the stats (singleton) gameobject);theQ$$anonymous$$(ref variable to quest manager)is null,so they give nullref exception when walked on.


 when starting from the second scene the game doesnt work?

No,the game works correctly in the first loaded scene.Things get messy only when changing scene

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;
     }
 }
avatar image zereda-games zereda-games · Feb 16, 2019 at 02:51 AM 0
Share

only read first few lines and it sounded right away like they are using a singleton method to have presistent data? is that correct? if so make sure you DontDestroyOnLoad is set to true. otherwise when you switch scenes it will destroy. if you have a singleton method and the object is in both scenes, whichever one was created first will survive and the others will automatically destroy on scene changes.

avatar image zereda-games zereda-games · Feb 16, 2019 at 02:52 AM 0
Share

ok i read the rest thats exactly whats going on your singlton method must be broken would you like to post the singleton method that goes in the awake?

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.

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

Problem with instantiate and destroy multiple objects 0 Answers

prefab/instantiate problem. C#,Instanciate/Prefab problem. 0 Answers

Can you configure prefabs in code? 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