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 SpectralEdge · Dec 02, 2011 at 06:47 PM · gameobjectliststaticsingleton

Static List< GameObject> in Singleton

I am using this code...

 using UnityEngine;

using System.Collections; using System.Collections.Generic;

public class TextManager : MonoBehaviour { private static TextManager instance;

 public static TextManager Instance
 {
     get
     {
         if (instance == null)
         {
             instance = new GameObject ("TextManager").AddComponent<TextManager> ();
         }

         return instance;
     }
 }
 
 void Start()
 {
     Letters = new List<GameObject>();    
 }
 
 public static List<GameObject> Letters;
 
 
 public static GameObject MakeWord(string Text)
 {
     char[] letrs = Text.ToCharArray();
     foreach(char letr in letrs)
     {
         foreach(GameObject letter in Letters)
         {
             if(letter.name == letr.ToString())
                 Debug.Log(letr);
         }
     }
     return null;
 }
 
 void OnDisable()
 {
     instance = null;
 }

}

I drop the script on a game object so I can add meshes I am using as 3d text to it.

But, the list does not show up on the object for drag'n'drop. I have used singletons before in c# but not in unity, am I doing it wrong? Or can I not use static lists of game objects? or? There are no errors, the list simply does not show up in the editor.

PS. I have edited this thing three times and cant get the code to show up correctly.

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 jahroy · Dec 02, 2011 at 08:28 PM 1
Share

It's a huge pain in the butt to get the code to format sometimes...

You might have to copy/paste it elsewhere, then paste it back.

1 Reply

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

Answer by WillTAtl · Dec 02, 2011 at 07:16 PM

static members don't show up in the inspector, but since the class is a singleton, the list doesn't need to be static, there'll only be one copy accessed through the single instance.

Note even this this probably isn't going to work like you expect; you can only inspect isntances of an object on an object in the scene, and this script doesn't exist on an object until some other script triggers it's creation through the Instance getter. This means you'll only be able to inspect and modify the list at run-time.

Myself, I usually don't bother with this style of singleton pattern, I just place a single instance on a special object in the editor with an exposed public static instance member that is set in Awake, and throw an error if instance is already set when awake is called, to detect cases where I screwed up and put more than one in the scene.

basically, this:

public class MySingleton : MonoBehaviour { public static MySingleton instance=null;

 void Awake()
 {
     if (instance!=null)
        Debug.LogError("You created multiple instances of MySingleton!");
     instance=this;
 }

};

Comment
Add comment · Show 11 · 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 SpectralEdge · Dec 02, 2011 at 07:34 PM 0
Share

The list has to be static or it throws an error.

So your suggestion is..do as I am doing except ins$$anonymous$$d of making the class singleton, simply throw an error on awake if there is an instance of it out there?

avatar image WillTAtl · Dec 02, 2011 at 07:40 PM 2
Share

The error I assume you're referring to is in $$anonymous$$akeWord where you access Letters directly, you just change that to instance.Letters, or change the function to a non-static function.

There's two basic approaches to singletons: $$anonymous$$ake everything static, or prevent making more than one instance and provide a way for anyone to get it. In unity, if you go the "everything static" approach, you won't be able to do anything to the object in the inspector at all, because static members never appear there. So you need an instance, if you want to inspect it, and if you want to be able to modify it in the inspector while editing, ins$$anonymous$$d of just while playing, you need the members to be non-static and you need the single instance to be placed in the scene directly ins$$anonymous$$d of creating and placing it while the game is running.

You can do it however you like, I'm sure there are others who will object to this informal approach as well, but the simplest solution with the least lines of code is the usually the best solution in my book.

avatar image SpectralEdge · Dec 02, 2011 at 08:07 PM 0
Share

Really all I want is to be able to create text from the files. I can't seem to access them from code without dropping them in a gameobject, I assume it has to do something with asset strea$$anonymous$$g which is pro only. I plan to get pro but only once the program is closer to completion as the investor (my husband, hah) will not allow the purchase of a pro licence till I have the prototype done :P

avatar image WillTAtl · Dec 02, 2011 at 08:55 PM 1
Share

tangental to the q, but making prefabs won't break the blender connection. Prefabs won't copy the mesh imported from blender, just reference it, so they will still update automatically :)

avatar image jahroy · Dec 03, 2011 at 12:39 AM 1
Share

You can +1 him now ;)

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

Why should I use a game object for non-physical things? 2 Answers

Don't render GameObject on load if already hit by player? 1 Answer

Adding a prefab to gameObject at a certain position in runtime 0 Answers

contents of a static list - see in editor 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