Advice on design patterns in C# for new user

Hi all.

I have very recently started using Unity after some years as a .net / as3 programmer and web guy.
Having just finished the excellent SimpleGame 2d shooter tutorial from 3dbuzz, I am keen to start on my own projects.
I am looking for tutorials and advice on best practices with Unity and OO. I am much happier in an OO environment and envisage using a GameStateManager singleton and dynamically adding and removing objects to the scene / game based on logic.

As an opening question, would it be appropriate to have a Manager script attached to the main camera?

Thanks a lot,

Dave

You can implement whatever design patterns you like, it’s really not that important. I would suggest that you instead create an empty Game Object called “Manager” or something and attach your game-wide control scripts to that, not the main camera.

Do whatever is going to work best for your project or coding style, but don’t fall into the trap of letting “design patterns” rule your decisions, especially something as abused as the singleton idea. It makes sense to me to attach scripts to your game objects to control that particular object, but you’re going to want some kind of global object somewhere that holds manager scripts, yes.

Hey xomg.

An empty Game Object makes complete sense for the manager, thanks.
Is it common practice to use empty GameObjects for a lot of things in Unity? One example I can think of is for audo. I would prefer to manage audio separately from my objects and then dynamically add and manipulate these as required. A good plan?

Again, it doesn’t really matter if you’re more comfortable with a certain method. I would attach audio sources to the objects in the game, no matter how they’re being spawned or created, as it just makes sense to me. For game-wide audio like music, then you could certainly handle that from a “game manager” object, or alternatively you could just attach an audio emitter to the player’s prefab and do it from there.

Lets say you’re spawning an alien and he needs to emit a sound - drag your alien into the scene and attach an audio emitter to him, then save him as a prefab back into your project assets. Then, when you spawn his prefab (from your Manager object) he will pop into the world containing an audio emitter and whatever other scripts you’ve attached to him.

I tend to have a “Game Controller” object in the scene to manage things like creating enemies or managing game-wide rules and state changes, and then another “Network Manager” object to handle that side of things, etc. In general I would encourage you to instantiate prefabs that have scripts attached to them to manage things like their AI, movement, and audio emission, not try to do everything from a controller object.

edit: check out some of the example projects on the Unity site, like the first and third person games or the networking ones. They should import okay into Unity 3, and you’ll see how the dudes at Unity tend to handle stuff like this. In general each of the games will have a couple of empty objects in the scene to handle exactly the kind of stuff that you’re thinking of. If it’s good enough for them, it’s good enough for me!

Thanks for the continued response. Your structure sounds very close to how I intend to do things. :slight_smile:

I have been playing with a simple GameManager class and instantiating Prefabs. It appears that unless you have a Public GameObject reference in your class that you fill with a Prefab in the inspector, you have no runtime access to dynamically adding Prefabs? :frowning:

To reiterate, if I have 200 individual Prefabs that I wish to add to the scene depending on certain conditions, I have to make 200 public variables for each of them in the parent class? If the answer to this is true, is there no way to dynamically load resources (such as audio) from the project and keep references to them at runtime?

I think I may have answered my previous question. Resources.Load() appears to do what I need. I will test it now

Thanks :slight_smile:

Hy all,
I am interested in a project named ‘3D password Authentication’. In it there will be a 3d virtual environment and there will be objects in it and the user will interact with those objects for authentication process.
So, my question is if it is possible to create a virtual environment in unity and then integrate that environment with java / C# and also with the Sql for database and all this will then be converted to a desktop application.
Please reply. Thanks in advance.