Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
2
Question by AwesomeFaceHD · Jul 02, 2016 at 11:50 PM · c#inventoryclasses

Can't access methods from class

So I have this script with two classes: using UnityEngine; using System.Collections;

 public class GameItem {
 
     public string ItemName { get; set; }
 
     public string ItemDesc { get; set; }
 
     public Item_Type ItemType { get; set; }
 
     public int ItemValue { get; set; }
 
     public int ItemID { get; set; }
 
     public GameItem () {
 
     }
 
     public GameItem (string name, string desc, Item_Type type, int value, int id) {
         this.ItemName = name;
         this.ItemDesc = desc;
         this.ItemType = type;
         this.ItemValue = value;
         this.ItemID = id;
     }
 }
 
 public class Weapon : GameItem {
 
     public string WeapEnch { get; set; }
 
     public float WeapEffic { get; set; }
 
     public Weapon () {
 
     }
 
     public Weapon (string name, string desc, string ench, float effic, int value, int id) {
         this.ItemType = Item_Type.WEAPON;
         this.ItemName = name;
         this.ItemDesc = desc;
         this.WeapEnch = ench;
         this.WeapEffic = effic;
         this.ItemValue = value;
         this.ItemID = id;
     }
 
     public void Attack () {
         Debug.Log("Attack tho");
     }
 }
 
 public enum Item_Type {
     APPAREL,
     WEAPON,
     TOOL,
     CONSUMABLE,
     BOOK,
     MISC
 }

And then I have a script for adding items to a list using the constructors:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class InventoryManager : MonoBehaviour {
 
     public List<GameItem> GameItems { get; set; }
 
     void Start () {
         GameItems.Add(new Weapon("Sword.", "It's a sword.", null, 5.0f, 15, 0));
     }
     
     void Update () {
         if(Input.GetButtonDown("Interact")) {
             GameItems[0].Attack();  // can't do this
         }
     }
 }

So when I create a new Weapon in the list, I can access everything from the GameItem class, but nothing from the Weapon class, even though I used the Weapon constructor. So I can't use the Attack () function, or access the weapon's variables. How can I get around this problem? Or how can I design the system to where I can use the methods and variables from all the classes that inherit from GameItem?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
3

Answer by Bunny83 · Jul 03, 2016 at 12:31 AM

You can't access anything from Weapon without a cast. The point of inheritance is to be able to treat objects in a general / generic way. So a GameItem reference might contain a Weapon but doesn't have to. The compiler doesn't know the type at compile time so you can't call Attack since that method doesn't exist in GameItem.

So solution one is to as-cast the type like this:

 if(Input.GetButtonDown("Interact")) {
     Weapon wep = GameItems[0] as Weapon;
     if (wep != null)
         wep.Attack();
 }

The second solution would be to use an "is" check and a normal cast:

 if(Input.GetButtonDown("Interact")) {
     if (GameItems[0] is Weapon)
         ((Weapon)GameItems[0]).Attack();
 }

Finally it's also possible to declare a virtual Attack method inside the GameItem class which the Weapon class can override:

 public class GameItem {
     // [ ... ]
     public virtual void Attack()
     {
         // Do nothing
     }
 }
 
 public Weapon : GameItem
 {
     // [ ... ]
     public override void Attack()
     {
         Debug.Log("Attack!");
     }
 }

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

162 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 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 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 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 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 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

Display list as UI or GUI 0 Answers

How to convert object types? 0 Answers

Custom class, Null Reference Exception 4 Answers

Using StartCoroutine in a Nested Class 0 Answers

Using a subclass in the inspector in place of a parent class and accessing the subclass variables 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