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
1
Question by cregox · Sep 18, 2013 at 08:17 PM · inspectorassetssingleton

Setting singleton assets through inspector

So, I was trying to add a NGUI's UIFont prefab into my singleton. Only to find out it's not that easy.

I could add the prefab on Resources and initialize it on Start. That would work. But I didn't want to do it like this because then I'll have to "lock" the prefab name to a string.

Rather, I was trying to figure out a way to do it on the Inspector. Then I got stuck. It simply doesn't work. Maybe it's a bug. If I add my singleton into a scene, the default values for language will load just fine. But when it's instantiated through script, nada.

Anyway, now here I am. Don't even know what'd be the best approach to do it.

Is this a failure on the Singleton design?

I mean, we can and should set up most singleton variables on the source code itself. But for some assets it seems to make more sense to do it on the Inspector. I just can't picture how.

Comment
Add comment
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

1 Reply

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

Answer by Bunny83 · Sep 18, 2013 at 08:57 PM

You can use a prefab-based-singleton. It requires you to place the singleton class on a prefab inside a resources folder to ensure that the prefab is included in the build.

You have to change the "Singleton" class to not create an empty GameObject but instead use Resources.FindObjectsOfTypeAll to instantiate the prefab

Comment
Add comment · Show 7 · 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 cregox · Sep 18, 2013 at 09:16 PM 0
Share

This seems to be almost perfect! Still troublesome if there is more than one type... Right now, I'll just throw a warning about it but, do you know if we can assign it somehow?

avatar image Bunny83 · Sep 18, 2013 at 09:47 PM 0
Share

No, in Unity you can always create multiple versions of an asset. Since serialized data can only be stored in assets it's not possible to prevent multiple instances.

avatar image cregox · Sep 18, 2013 at 09:57 PM 0
Share

Not sure if you got what I meant. I wanted to assign the object from resource into the singleton somehow. So even if there are multiple versions of an asset, I could assign only one. In any case, this mostly solved my problem as it is! And I'll update the wiki after you give me a conclusive answer about this aspect. :)

avatar image Bunny83 · Sep 18, 2013 at 10:14 PM 0
Share

I'm not sure what you mean by "assign into the singleton". A prefab-based singleton class would look like this:

 if (_instance == null)
 {
     _instance = (T) FindObjectOfType(typeof(T));
     if (_instance == null)
     {
         T prefab = (T)Resources.FindObjectsOfTypeAll(typeof(T));
         if (prefab != null)
         {
             _instance = (T)Instantiate(prefab);
         }
         else
         {
             _instance = (new GameObject()).Addcomponent<T>();
         }
         _instance.gameObject.name = "(singleton) " + typeof(T).ToString();        }
         DontDestroyOnLoad(_instance.gameObject);
     }
 }
 return _instance;
avatar image cregox · Sep 18, 2013 at 10:18 PM 0
Share

Yeah, I thought that code wouldn't actually work. Because it is FindObjects, plural. T prefab would have to be T[]. The precise problem I pointed in the first comment, and reason why I'd want to "assign it somehow", ins$$anonymous$$d. But it works. Compiles, at least. Although, I have no clue why, suddenly it stopped working for me. In any case, the problem of finding more than 1 still exists.

By "assign into singleton" I meant like we can assign into a public T of a normal script - be it on the prefab or on the script inspector window.

Show more comments

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

16 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

Related Questions

Cannot assign assets to variables via inspector: NullReferenceException 3 Answers

Singleton & Inspector 1 Answer

Inspector:Reference a component in Assets, not in scene. 0 Answers

Setting default import settings? 1 Answer

CustomEditor for an asset group -- is it possible? 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