Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 YahiaDro00 · Feb 22 at 12:33 PM · staticnullsingleton

Static singleton is (not nullref -empty-) but assigned to "null" in the other Scenes

as Mentioned in title a weird effect of going to next scene by static variable singleton I always use singleton and its always working correctly but not this time .

I'm not using dontDestroyOnLoad of course cuz its a static var

I can work around the issue by using dontDestroy but am here to learn sth new about singeltons any body could help? Thanks

TheCode

 public static DashBoard_Ref DbR_Instance;
 void Start()
         {DbR_Instance = this; }

forget to say its working in calling its methods (no nullref excep) but the problem showed up to me when I intended to use {If check} --> if(DashBoard_Ref.DbR_Instance != null) its false means its null but with no nullexception -when using methods without if- as mentioned ..

Comment
Add comment · Show 1
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 YahiaDro00 · Feb 22 at 12:37 PM 0
Share

forget to say its working in calling its methods (no nullref excep) but the problem showed up to me when I intended to use {If check} --> if(DashBoard_Ref.DbR_Instance != null) its false means its null but with no nullexception when using methods without if as mentioned ..

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Spip5 · Feb 24 at 12:09 AM

Have you tried making it a static class instead ? I don't see the full script, but I suppose it is running in a Monobehaviour.

I'm not using dontDestroyOnLoad of course cuz its a static var

The both are not related. For a Singleton instance of a monobehavior class, the instance acts as a "link" to a component, which is attached to a gameObject.

Should this object be deleted (changing a scene for example), then your singleton instance returns null. If you do not need the monobehavior components then do a static class

 public static class DbR
 {
     // your functions and variables
 }

and access it with "Dbr.myVariable" or "Dbr.myFunction()" . This way, it will act as a singleton, and won't be impacted by scenes changes, because it won't be attached to a GameObject (because it is not a monobehavior). If you need it to be a monobehavior, then either use DontDestroyOnLoad or load your next scene additilvely (more complexe).

Comment
Add comment · Show 2 · 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 YahiaDro00 · Feb 24 at 08:10 AM 0
Share

ok nice so instance singleton is just an address for the Monobehaviour which is not still running while in another scene , I just don't know how it was working before with same weathers between scenes and don't know how without using the if statement its all running right

DashBoard_Ref.DbR_Instance.DoSth(); // that's working correctly and also not giving null excep

Thanks

avatar image Spip5 YahiaDro00 · Feb 24 at 09:49 AM 0
Share

hard to say without the code. Can you share your DashBoard_Ref script ? Also, can you share the line where the new scene is loaded ? EDIT : in your new scene, you do not have a gameobject with this script, right ?

avatar image
1

Answer by haruna9x · Feb 24 at 04:11 AM

This problem is caused by the order in which the scripts are executed. If you call DashBoard_Ref.DbR_Instance before DashBoard_Ref's Start() function is called, it will be null. To fix this problem, you can assign static variable in Awake() function, and use it only in Start() function. You can also edit the order in which the scripts are executed (not recommended). Order of execution for event functions

Comment
Add comment · Show 5 · 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 YahiaDro00 · Feb 24 at 08:02 AM 0
Share

thanks , but am using the instance in another scene after that Dashboard already being created before . i did also tried using Awake() its the same

avatar image haruna9x YahiaDro00 · Feb 24 at 08:18 AM 0
Share

Are you sure that there is at least one instance of the DashBoard_Ref component that is active on the Hierarchy window after you have switched the scene. Because you are not using DontDesstroyOnLoad on the gameObject, by default it will be destroyed, and DbR_Instance will now become null.

avatar image YahiaDro00 haruna9x · Feb 24 at 09:20 AM 0
Share

true it must refers to a null , but what I was confused by is why its working in calling function & why also there is no null exception -: shBoard_Ref.DbR_Instance.DoSth(); // that's working correctly and also not giving null excep


so I was assuming (bcuz this working) that instance(singleton) is not working like an address refers to a comp, but an clone object that caches everything in the component

I think I know now , will try to explain it later i

avatar image haruna9x YahiaDro00 · Feb 24 at 08:25 AM 0
Share

If you want to talk about the check null operator, this blog can help.

avatar image YahiaDro00 haruna9x · Feb 24 at 09:20 AM 0
Share

that will be helpful , thank you

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

135 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

Related Questions

Static reference cost 1 Answer

Static Array - Throwing NullReference 1 Answer

Unity : Singleton ScriptableObjects resets after play 1 Answer

Static List< GameObject> in Singleton 1 Answer

In which cases I should use static? 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