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 Ibzy · Jan 18, 2014 at 11:37 PM · c#subclass

How to create an 'Item' subclass

Hi,

I recently received some great help with a question about subclasses and my approach to various item types.

I am now struggling with creating my items - please see below example:

 public Item(string name, int cost, string type, string desc){
     _Name = name;
     _Cost = cost;
     _Type = type;
     _Desc = desc;
 }

The above is in the Item class (Item.cs) and defines an Item, so to create one I use: new Item("Item",100,"Consumable","This is an Item")


public class ArmourUpgrade : Item {
public ArmourUpgrade(){
        _UpgradeValue = 0;
    }
}

I then have this in the ArmourUpgrade class (ArmourUpgrade.cs)...how do I create an ArmourUpgrade in the same way as above?

I'm sure its a simple answer, but I just can't see it.

Thanks

EDIT:

Update to the question above..

 public ArmourUpgrade(string name, int cost, string type, string desc,int UpgVal){
         Name = name;
         Cost = cost;
         Type = type;
         Desc = desc;
         UpgradeValue = UpgVal;
     }

The above code works, so from my Shop script I call ArmourUpgrade("Upgrade v1",5,"Upgrade","This is Upgrade v1","10"), but as al Items have a name, cost, type and desc, does this need to be included in the ArmourUpgrade constructor?

Thanks again.

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

2 Replies

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

Answer by Uncasid · Jan 20, 2014 at 04:25 PM

A little tip I learned in college:

Sit down and write out what each item is, their attributes and everything else. When done, take that information and look for common things, like "Name" or "Price", make this it's own class. You then inherit this and start the granulate the concepts down. Here is an example that might help

 public class GameItem
 {
 
     public string Name { get; set; }
 
     public int Cost { get; set; }
 
     public GameItem(string name, int cost)
     {
         Name = name;
         Cost = cost;
     }
 
 }
 
 public class Weildable : GameItem
 {
 
     public enum WeildLocation {
         head,
         shoulder,
         hand
     }
 
     public WeildLocation WeildableLocation { get; set; }
 
     public Weildable(WeildLocation location, string name, int cost)
         : base(name, cost)
     {
         WeildableLocation = location;
     }
 
 }
 
 public class Weapon : Weildable
 {
     public Weapon(string name, int cost) : base(WeildLocation.hand, name, cost) { }
 }
 
 public class Helmut : Weildable
 {
     public Helmut(string name, int cost) : base(WeildLocation.head, name, cost) { }
 }
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 Ibzy · Jan 20, 2014 at 04:49 PM 0
Share

Hi Uncasid,

This appears to confirm what I was beginning to think - I need my subclass constructor to receive all arguments and then feed them through to the base class. Now that it makes sense, I'm wondering why I was confused to begin with.

Thanks

avatar image Uncasid · Jan 20, 2014 at 04:53 PM 0
Share

It happens, you just need to understand how it works in an easier context. $$anonymous$$SDN can be daunting to newer programmers. $$anonymous$$y suggestion to you is to play around with the class types in c#. Try out the interface, try out the abstract. See how they act in certain sittuations. They have their own uses :)

avatar image
1

Answer by getyour411 · Jan 18, 2014 at 11:47 PM

This helps explain inheritance and constructors http://unity3d.com/learn/tutorials/modules/intermediate/scripting/inheritance

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 Ibzy · Jan 19, 2014 at 10:17 AM 0
Share

Thanks, getyour411, this helps explain how it works (parent class is called first) however I'm still unsure as to how a constructor would work if the constructor for the subclass takes more arguments? ArmourUpgrade(name, cost, type, desc, upgradeVal) I imagine doesn't work?

avatar image Uncasid · Jan 20, 2014 at 04:03 PM 0
Share

It would work, you would just have to supply them, then populate using a base constructor:

 class Bar {
     public Bar(double param1, bool param2) {
     }
 }
 
 class Foo : Bar
 {
     public Foo(double param1)
         : base(param1, true)
     {
     }
 }

Here is some $$anonymous$$SDN love for ya: http://msdn.microsoft.com/en-us/library/k6sa6h87.aspx

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

19 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 avatar image avatar image

Related Questions

Accessing CharacterMotor 1 Answer

I cannot use Native Plugins using C on iOS 0 Answers

Serializing specialized subclasses of generic classes not working 1 Answer

Adding a box collider to an object in csharp script 3 Answers

Unity won't allow animations 1 Answer


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