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 Exce51 · Aug 12, 2013 at 03:10 PM · instantiateclassesconstructor

When should I use constructors VS using Awake() or Start()?

http://unity3d.com/learn/tutorials/modules/beginner/scripting/classes

After watching the above tutorial it left me a bit confused. I understand how classes, instantiation and constructors work. What I don't get is why the tutorial suggested having a "stuff" class inside of the "inventory" class with a "stuff" constructor. Why not just make the inventory class and use its start or awake methods to load your inventory?

Is it because the constructor allows you to overload it with different parameters? If I were to have a class within a class (such as with this example), could I still run the Update() method on it?

My confusion stems from my use of C#/XNA. In there you have a main game loop that you can call instantiated objects in. With Unity, I guess you put these instantiated objects in an empty game object in the game (assuming the class is for storing game data rather than cotrolling a 3D asset)?

Edit: Also of note the scripting reference states: "Note for C# and Boo users: use Awake instead of the constructor for initialization, as the serialized state of the component is undefined at construction time. Awake is called once, just like the constructor." which is contradictory to the suggestions of the first link..

http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.Awake.html

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 ArkaneX · Aug 12, 2013 at 03:51 PM

You can of course do all this in the Inventory class. You can create a number of properties and keep the grenades, bullets, etc. in them. In some games inventory could also have additional properties like AvailableSlots, UsedSlots, IsUpgradable, CanAccess etc. Creating separate class just for the items you have in this inventory, is just a convenient way to have some separation from those other properties.

As to class nesting - you don't have to do it - you can put Stuff class in a separate script, which would be helpful if you want to use it in different places, e.g. in Shopkeeper script, Chest script, etc. Nesting like in this example would be useful if you decided that Stuff will be available to Character only.

Awake() vs constructor: if you work with class derived from MonoBehaviour, use Awake(). If you work with class derived from ScriptableObject, use CreateInstance. In other cases use constructor. Please note, that in the given example, nested Stuff class derives from object, so constructor is required.

EDIT: Regarding the Update() - you can only use it in MonoBehaviour, so in the example you can use it in Inventory class, but not inside nested Stuff class.

Comment
Add comment · Show 1 · 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 Exce51 · Aug 12, 2013 at 04:29 PM 0
Share

Ah, I was unaware of the ScriptableObject class. So with this I can just derive from ScriptableObject and keep that script in the project folder without attaching it to any object? That makes much more sense.

avatar image
4

Answer by robertbu · Aug 12, 2013 at 03:58 PM

If your class is derived from Monobehaviour, then you use Awake() and/or Start() to do any initialization. They both only get called once per instance. Monobehaviours exist as components of game objects. To create one dynamically, you typically create a game object and then use GameObject.AddComponent() to add the Monobehavior script to the game object. If you have classes that don't require a game object, then don't derive them from Monobehaviour. If your class is not derived from Monobehaviour, then you use the class constructor for initialization.

The 'Stuff' class in the video doesn't require a game object. It does not need an Update() loop. You don't want the overhead of storing a whole game object with each instance of the class. So you just create a 'normal' class and use the a constructor for initialization.

Comment
Add comment · Show 1 · 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 Exce51 · Aug 12, 2013 at 04:30 PM 0
Share

Thanks for the reply. With all of the replies here I was able to figure out what excatly is going on.

avatar image
4

Answer by JamesB · Aug 12, 2013 at 04:20 PM

The video is meant to show some of the things that you can do with classes but not necessarily what you should. I'll try and clarify when you should use each of these functions.

Call order: Constructor, Awake, Start

Constructors - these should generally not be used on MonoBehaviour classes unless there is some operation that can only be done in a constructor. Instead use Awake as this is called immediately after. For non-MonoBehaviours constructors, it is generally best practice to use them for variable initialisation but this is by no means their only use. 'Best practice' in this area is debatable and so you should not feel limited.

Awake - This is a MonoBehaviour function that is called immediately as a script appears in a scene. This is called whether or not the script is enabled on it's instantiation. This is best used for any references that you want to set up on your script (such as any use of GetComponent.

Start - This is a MonoBehaviour function that is called once a script is enabled in a scene immediately before the first Update call. As a personal preference if I have no special reason to use Start as well as Awake, I will put all code in Awake. Under some circumstances it is best to use both Start and Awake but this are quite specific.

As for the video, it would be best to have either Stuff in another script or ignore it entirely. The purpose of the video was to show what was possible. Sorry if that was misleading.

Comment
Add comment · Show 1 · 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 Exce51 · Aug 12, 2013 at 04:30 PM 0
Share

Thanks for the reply. With all of the replies here I was able to figure out what excatly is going on.

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

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Creating a Loadout 1 Answer

Referencing the object that a construtor is currently constructing/working on. 1 Answer

Tetris Unity game structure C# 3 Answers

How to associate underlying data structure to prefab at runtime? 1 Answer

Classes, MonoBehaviours C# 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