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 tneshi · Feb 21, 2012 at 10:20 PM · c#errorinheritance

Passing arguments to base constructor fails

I have a Character class with a ragdoll and a HumanoidCharacter subclass that should set it.

 public class Character : MonoBehaviour {
     private Ragdoll ragdoll;
     
     public Character(Ragdoll ragdoll, int maxInventorySize) {
         if (ragdoll == null) throw new System.ArgumentNullException("ragdoll");
         this.ragdoll = ragdoll;
     }
 }

 public class HumanoidCharacter : Character {
     public HumanoidCharacter(int maxInventorySize) : base(new Ragdoll(new ItemSlot[] {new ItemSlot(ItemSlotType.RightHand)}), maxInventorySize) {
 }
 }

 public class PlayerCharacter : HumanoidCharacter {
     public PlayerCharacter() : base(32) {
     }
 }


I'm getting the ArgumentNullException for ragdoll when I attach PlayerCharacter to a GameObject.

I would have thought that PlayerCharacter's constructor is called, which calls HumanoidCharacter's sonstructor which then calls Character's constructor with the new Ragdoll. What's going wrong?

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 Bunny83 · Feb 22, 2012 at 03:48 PM 0
Share

This is an interesting question. Good you've fould already the solution ;)

The problem with the constructor is that it is called whenever you usually don't expect it. Everytime you save / load a scene in Unity or a script gets (re-)compiled, Unity recreates the objects and deserialize the serialized members to restore the "old state".

I don't see a direct reason why this setup shouldn't work.
What's the base class of Ragdoll? I guess it's a "usual" class derived from System.object? $$anonymous$$eep in $$anonymous$$d that you can't create $$anonymous$$onoBehaviours with "new" since it's part of the strategy-pattern. Components can only live on GameObjects and can only be added with AddComponent.

avatar image tneshi · Feb 22, 2012 at 05:00 PM 0
Share

Wait, maybe it it really was a $$anonymous$$onoBehavior. It isn't right now, but maybe I changed that while I hunted down the error and already forgot. It kind of resonates with some dark part of my memories, that got buried while I was working too late.

1 Reply

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

Answer by tneshi · Feb 22, 2012 at 03:35 PM

I have in the mean time read, that using Constructors for MonoBehavior is generally discouraged. I changed my code to set those variables in Awake instead. Now it works.

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.

Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

An OS design issue: File types associated with their appropriate programs 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Error when loading Unity WebGL 1 Answer

Can't access variable due to its protection level? 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