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 AwesomeFaceHD · Nov 13, 2014 at 05:19 AM · c#classinheritanceconstructors

Having problem with constructor

I'm making an inventory system and I want to code all the items in manually using a constructor, I find it more fun that way, but I'm getting an error.

Item Class:

 using UnityEngine;
 using System.Collections;
 
 [System.Serializable]
 public class Item {
     public string itemName;
     public string itemDesc;
     public Texture2D itemIcon;
     public int itemID;
     public int itemValue;
     public ItemType itemType;
     
     public enum ItemType {
         Weapon,
         Apparel,
         Consumable,
         Misc,
         Quest
     }
     
     public Item (string name, string desc, Texture2D icon, int id, int value, ItemType type) {
         itemName = name;
         itemDesc = desc;
         itemIcon = icon;
         itemID = id;
         itemValue = value;
         itemType = type;
     }
 }

Items List:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class Items : Item {
     public static Texture2D asdf;
     
     private Item bread = new Item("bread", "eat it", asdf, 0, 2, ItemType.Consumable);
 }

I'm inheriting "Item" in my Items list script. I though I had everything correct, but I get this error: error (The type "Item" does not contain a constructor that takes '0' arguments). I understand what it's saying, I just don't know how to fix it. I tried working around it, changing names and stuff, but nothing works. If you can see what I'm trying to do, can you please lead me in the right direction?

Comment
Add comment · Show 5
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 AlwaysSunny · Nov 13, 2014 at 05:24 AM 0
Share

Please consider posting code within UA using code tags, and pasting errors here too. Why would Items inherit from Item? That doesn't seem to make sense.

avatar image AwesomeFaceHD · Nov 13, 2014 at 05:29 AM 0
Share

@AlwaysSunny Edited.

avatar image AwesomeFaceHD · Nov 13, 2014 at 05:33 AM 0
Share

@AlwaysSunny I thought I needed to inherit from Item to use the variables to create new items? I'm still a noob, so I wouldn't be surprised if I was completely off.

avatar image AwesomeFaceHD · Nov 13, 2014 at 05:45 AM 0
Share

Okay, turns out I'm just dumb. The enum had to be outside of the Item class, then the Items class could recognize it without inheritance.

avatar image thomas1231 · Mar 31, 2017 at 02:47 PM 0
Share

Did you find a way to fix the error having the same problem

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by IEMatrixGuy · Nov 13, 2014 at 05:30 AM

Hi,

As far as I can see it's actually a pretty simple error.

An enum is not a function (as im sure you are aware), and hence requires for there to be a semi-colon after the line is finished, so after the closing }. This is probably why youre getting that error, because as far as the code is concerned, there is no valid constructor whatsoever.

Hope that helps :)

IEMatrixGuy

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 AwesomeFaceHD · Nov 13, 2014 at 05:35 AM 0
Share

http://puu.sh/cOJIz/a62ab54562.png I added one there, if that's what you were talking about and I'm still getting the error.

avatar image
0

Answer by Owen-Reynolds · Nov 13, 2014 at 04:39 PM

This is a standard C# error -- nothing to to with Unity. There are lots of places to read how C# classes work, and what the technical terms and errors mean.

In general, if it doesn't involve a specific Unity function, like rayCast or SendMessage, the best place to find the answer is a general google search with C# and the error. Sometimes even if it does involve something Unity-specific, it's still just a common C# error. Think of Unity as middle-wear. There's a bunch of stuff everyone knows already, with dozens of helpful places to learn, like modelling, textures, C# programming; and Unity mostly just combines them in a pretty IDE.

Now, in order to learn anything, you need some "real" project to try it on. And nothing wrong with using Unity to learn how to write classes. But I think you'll be a lot happier if you think of your current project that way -- "teaching myself C# classes." For fun, try the sentence "teaching myself C# classes, and enums, and Unity-C# addons, and making an inventory system." Doesn't roll off the tongue.

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

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

Behind the scenes voodoo w/ Instantiate 2 Answers

C# Array of Child Objects 1 Answer

Parent Class variables in Child Class's Inspector (C#) 0 Answers

Inheritance best solution to handle *many* instances of a class with unique values? 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