Search Unity

Management Class with an Arraylist in C#

Discussion in 'Scripting' started by Tabu, Jul 2, 2010.

  1. Tabu

    Tabu

    Joined:
    Dec 3, 2008
    Posts:
    44
    Hey
    I have been experimenting with C# for a few months now (My trade is Art, but hey, programming is also fun :) )
    I'm trying to make a simpel "smash em' up" game (Ala. Double Dragon) with Vikings. However, I stumbled into a problem, adding objects (Opponents) to a Arraylist in a management-class using a singelton pattern, if that makes sense? Apparently, the singelton pattern does not go well with Unity.. so my question is this. Is there any other way to initialise a script as a single instance, other than adding it to a gameobject?

    Thanks for your time :)
     
  2. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    There is actually a way around this in C#. You can define a static initialiser on a class:-
    Code (csharp):
    1. public class MyClass {
    2.     static MyClass() {
    3.         // Initialise static variables.
    4.     }
    5. }
    You can only initialise static variables with this since there is no instance available. It is guaranteed to be called before anything uses the variables.
     
  3. Chris-Sinclair

    Chris-Sinclair

    Joined:
    Jun 14, 2010
    Posts:
    1,326
    I'm using plenty of static classes and static members in C# Unity right now and having no issues whatsoever. Perhaps you can post some code or be more specific as to how you're implementing your singleton?
     
  4. Tabu

    Tabu

    Joined:
    Dec 3, 2008
    Posts:
    44
    Thanks for the answers guys, they really helped me, have been having holidays so "Kids > Unity" hence the late reply.

    I solved the problem, and kinda removed the manager script, and removed the code from the other scripts.
    Pretty much used the ideas from this page: http://en.wikipedia.org/wiki/Singleton_pattern#C.23
    I wanted to recreate the scripts, and post them here, but diden't get arount to it.

    If you really would like to see it, I will recreate and post it?
     
  5. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    Any reason you're using ArrayList over Generic List?
     
  6. Tabu

    Tabu

    Joined:
    Dec 3, 2008
    Posts:
    44
    hehe, I guess this video sums up the reason, just replace "Budapest" with "Generic list" and "European Country" with "Arrays and Hashtables" Budapest?!?

    On a more serious note, thanks for the tips, reading up on it now at unifycommunity.comhopefully this will help me in the future when trying to decide how to solve problems. Thanks
     
  7. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    :)

    In that case, I recommend reading this friendly primer. I'm no authority, but I've tried to do a lot of research on the subject, and everything I've found has basically said, "People still use ArrayLists because they learned to use them a long time ago, and they do the job. But Generic Lists have made them obsolete - ArrayLists are comparatively slow, and only kept around for legacy compatibility (read bloat)."