x


Scripts/code sitting outside of individual scenes

Hey,

I am new to Unity but have some experience of coding in C++ and C#. I am currently trying to get my head around the best way of structuring code with Unity, and understand the way that scenes, assets and components work.

However, I am a little stumped with how executable code can sit outside of a particular scene, and if it does how it is instantiated and referenced from other code. The type of thing that would typically fall under this category would be 'Manager' type classes that need to be accessible in a lot of places in code. I understand that the need for these is significantly reduced with Unity - this is one of the reasons I have decided to use it, but I still have a need for this type of thing.

I would be extremely grateful for any pointers as to how this should be done.

Wibbs

more ▼

asked Feb 01 '11 at 06:29 PM

Wibbs gravatar image

Wibbs
1 1 1 1

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

I'm always have a special scene with my manager scripts in it. That scene is loaded first and gets never loaded again. Within your script you should cal DontDestroyOnLoad() on your gameobject. That will prevent that gameobject (and all childs and scripts that are attached) from being destroyed at LoadLevel.

You may also consider a "unity style singleton". There are many ways to implement a singleton. unsave, save and very save, but it depends on what you need.

I'll guess you use C#

// unsave
public class MyGame : MonoBehaviour
{
   public static MyGame instance = null;
   void Awake()
   {
      instance = this;
   }
}

Or the better one:

// save
public class MyGame : MonoBehaviour
{
   private static MyGame m_Instance = null;
   public static MyGame instance
   {
      get 
      {
         if (m_Instance == null)
            m_Instance = (MyGame)FindObjectOfType(typeof(MyGame));
         return m_Instance;
      }
   }
}

Here the "super save" version, but it don't make much sense because most of time you need to setup some references to prefabs and stuff in the editor. If you create that GO from scratch it would be "naked".

// super save
public class MyGame : MonoBehaviour
{
   private static MyGame m_Instance = null;
   public static MyGame instance
   {
      get 
      {
         if (m_Instance == null)
         {
            m_Instance = (MyGame)FindObjectOfType(typeof(MyGame));
            if (m_Instance == null)
            {
               GameObject GO = new GameObject("MyGame");
               m_Instance = GO.AddComponent<MyGame>();
            }
         }
         return m_Instance;
      }
   }
}
more ▼

answered Feb 01 '11 at 07:06 PM

Bunny83 gravatar image

Bunny83
45.4k 11 49 207

That's brilliant info from both of you and has told me exactly what I was after.

Thanks,

Wibbs

Feb 01 '11 at 09:16 PM Wibbs
(comments are locked)
10|3000 characters needed characters left

All scripts outside of folders named "Editor" are included in builds, even if they aren't used, as far as I know. (Most other assets can be included in the project but not be included in the build if they aren't necessary for the scene.) Regardless, even if I misinterpreted that fact at some point, there's no problem with what you want to do. At the very least, the scripts you need will be included. At least they always have been for me. ;-)

more ▼

answered Feb 01 '11 at 06:37 PM

Jessy gravatar image

Jessy
15.6k 72 95 196

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5086
x76

asked: Feb 01 '11 at 06:29 PM

Seen: 541 times

Last Updated: Feb 01 '11 at 06:29 PM