• 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
5
Question by knoggly · Feb 17, 2010 at 10:30 PM · gameobject

Game architecture

Hi,

I'm currently thinking about the Architecture of my new Unity-Game. I'm struggling with 2 options:

1) Create GameObjects with the Editor for Entities like Player, Enemy, Items and then attach some Scripts to this Objects.

2) Have a custom Master-Class (like Game) that holds all Game-specific data, like Players, Enemies, Gamestate, etc. And then create and attach the GameObjects to this custom-objects.

I think the second approach is a must if I want to create Objects during Runtime (e.g. random enemies). Is it even possible to create GameObjects from scratch (with ...new GameObject(...)), I just found solutions with cloning/coping existing Objects.

Has somebody any suggestions or options I have missed. (technical answers are welcome, I have some programming background ^^)

regards knoggly

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

3 Replies

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

Answer by Murcho · Feb 17, 2010 at 10:49 PM

Both are viable options, and it really comes down to your game and it's complexity.

With option one, you as the designer have full control over everything you do, however you also end up having to do all of the work in a repetitive and tedious manner. This is probably the safest way to work, but if you have a background in programming, you can create a better way.

With option two, you can have a "Factory" class, it could be a single factory that generates everything, or you could have a separate factory for each object type you need to spawn. You can hold a reference to each prefab you need to spawn in these factories, and simply use Instantiate() to generate your enemies, players, etc. The problem with this approach is your code needs to know where to place these, otherwise it will end up defaulting their spawn point to the origin, and that can get a little predictable.

To counter this, you can easily make a little Editor tool to place spawn points around a level, and programatically select a spawn point for each Instantiation you perform. It sounds like a long way around, but over the course of your entire game production, it could save you a lot of time.

As for your last question, creating GameObjects from scratch, it is possible. Here's a quick example.

// Create the gameobject
GameObject newObject = new GameObject();
// Add the required components
newObject.AddComponent<Component1>();
newObject.AddComponent<Component2>();
// Set it's position in the world
newObject.transform.position = new Vector3(50, 1, 50);

Obviously you could do a lot more with it, but I would probably recommend cloning prefabs with Instantiate, as writing a tonne of code to make what is essentially a prefab every time is a bit of a waste. On the flip side, if you are randomizing the abilities of enemies on the fly before spawning them, then this way could be a great option to take.

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

Answer by knoggly · Feb 18, 2010 at 07:12 AM

Hi,

thanks for your Answers, I think (surprise, surprise) I will use a combination of both methods :). Create Stubs/Templates for all visual objects, place them somewhere offscreen and then "clone-them-in" on use. I think some data management aside in custom c#-objects is also neccessary to store states across Scene-changes.

regards knoggly

Comment
Add comment · Show 2 · 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 Jaap Kreijkamp · Feb 18, 2010 at 07:20 AM 2
Share

Ins$$anonymous$$d of stubs placed off screen, use Prefabs and instantiate them when needed!

avatar image knoggly · Feb 18, 2010 at 09:14 PM 0
Share

Alright, I thought this is the same. So Prefabs are more like classes?!

avatar image
0

Answer by Litobyte Softworks · Feb 18, 2010 at 03:18 AM

I think it strongly depends from the design of your game...

EG: our game is very complex, but for level design issues, we HAD to use the unity bult-in editor to build all the levels (differently we'd have to serialize a graphic package scene or other stuff, and this is a no-no for fast development), but we do use the Resource folder, loading the objects dynamically from scripts when needed, for particles, and other stuff..

The best bet with unity is working strongly with prefabs, instanced in the scene, and have the dynamic ones placed in the Resources folder.

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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

DontDestory gameobject doesn't work on button click after a new scene loads 2 Answers

Unity Detonator Problems 1 Answer

OnDestroy collision detect 4 Answers

gameObject are not referenced 2 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