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
0
Question by dustxx · Jul 22, 2014 at 05:48 AM · c#variableserializationconstructorsmonobehavior

How do you use serialized values at creation?

For instance, let's say I have a class called "PlayerStats" which is attached as part of some player monobehaviour. "PlayerStats" has one int field representing maximum health. This is serialized so it is editable in the inspector. I also have a variable called current health (not serialized), and I want this variable to start out at the value of the maximum health input by the inspector. The default constructor is, of course, called during deserialization upon starting the game so it seems that the logical thing to do would be to set the "current health" to the "maximum health" in the default constructor. However, if I try and access the "maximum health" field during the constructor, it does not yet have its serialization information, so at the time of this call the "maximum health" will still be equal to 0 and not whatever was input in the inspector. These values are not set until AFTER the constructor is completed. What is the proper way to do something like this?

Comment
Add comment · Show 2
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 calebheale · Jul 22, 2014 at 06:49 AM 0
Share

How familiar are you with serialization? Not to sound rude but what you are describing doesn't sound like a proper implementation of serialization, at least as far as I know.

This is serialized so it is editable in the inspector.

Once something is serialized it wouldn't be editable in the inspector.

The default constructor is, of course, called during deserialization

Deserialization could happen in the constructor but not the other way around.

Not trying to pick on you, just trying to see where our descriptions differ so I can try and help. Have you watched the live training video on persistence? It is a pretty good place to start.

avatar image dustxx · Jul 22, 2014 at 02:54 PM 0
Share

Perhaps I was describing it wrong, so let me show you what I meant.

Once something is serialized it wouldn't be editable in the inspector.

I marked this class with the Serializable c# attribute, and marked several fields with SerializeField. Doing this causes an instance of the class to be editable in the inspector when said instance of the class is a serialized variable of a monobehavior and thus visible. This even works properly in game.

Deserialization could happen in the constructor but not the other way around.

I think I might've confused some terms on what I originally said, but what I meant is that when these instances are being created through this data inputted into the inspector as shown above (which I thought was 'deserialization'), a call to the default constructor is called. Here is the default constructor of the original serialized class. As you can see, calls are made to this constructor on startup for every instance of the original class that has been defined in the inspector. All I am trying to do is access the information passed in through the inspector during the creation of an instance. At the time that this constructor is called, the information from the inspector is not yet accessible so if I were to try to print the nickname field to the console, which I showed earlier to have a default value of "", it would write an empty string ins$$anonymous$$d of the inputted name. After the constructor though, that information works just fine as you saw in the battle screenshot.

Like my original question said, I would like to know how to use information passed in this way to assign things like default values based upon the passed information. The example I gave that is still a problem is if we said:

I also have a variable called current health (not serialized), and I want this variable to start out at the value of the maximum health input by the inspector.

Because the maximum health input by the inspector is not defined until after the constructor, I can't assign it to the current health. I actually work around this in my game by using a "damage" amount that starts at 0 and calculating the "current health" bu subtracting that from "maximum health", but I've thought of several cases where I'd like a default value to use information passed at startup.

Honestly, although I'm sure there's reasons why this isn't possible, I wish unity could just let you input the parameters for a constructor in the editor and create instances that way.

So, any thoughts? And maybe you could tell me the terms I'm looking for to describe this if they aren't serialization? $$anonymous$$aybe there's an entirely better way to go about this? Thanks!

1 Reply

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

Answer by WilliamLeu · Jul 22, 2014 at 05:13 PM

Do NOT EVER use or implement the constructor for MonoBehaviours. Use either Awake() or Start().

Awake is called when the behavior is first created (probably what you want to use for your case)

Start() is called the first frame a GameObject is visible (after awake).

Comment
Add comment · Show 3 · 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 dustxx · Jul 22, 2014 at 05:16 PM 0
Share

I don't think you read the question very well. At no point are constructors being used for monobehaviours. They are being used on regular c# classes. Instances of these classes exist on monobehaviors, but the monobehaviours themselves do not use constructors at all.

avatar image WilliamLeu · Jul 22, 2014 at 06:47 PM 0
Share

Touché, right you are. Luckily the answer is still the same but for different reasons. Iterate through those objects in the Awake() or Start() of the $$anonymous$$onoBehaviour and set the desired values.

avatar image dustxx · Jul 22, 2014 at 07:21 PM 0
Share

ohhhhh myyyyy goddddd I can't believe I never thought of using the monobehaviour to set the defaults of the objects they hold. I knew it was such a simple thing there had to be an easy way to do it. Next time I need this I'll just throw an init method on the object and call it for each component at the start. Damn! What an easy fix. Thanks a lot, sometimes I just need another $$anonymous$$d to look at something and see the simple solution I can't cause I've been overthinking it for too long hah. Anyways, thanks again!

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Serialize problem with Unity Editor, values reset on game execution! 1 Answer

Distribute terrain in zones 3 Answers

Change values of variables in System.Serializable 1 Answer

How to get a variable out of a coroutine? 3 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