• 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
0
Question by MercenarySed2 · Jun 01, 2016 at 08:46 PM · c#parentchildinheritancechildren

Why can't I access the public variables of the children class of TestItem in this syntax? I only have access to TestItem variables

 //used to retrieve an item by name
     Dictionary<string, TestItem> itemDatabase = new Dictionary<string, TestItem>();
     //used to retrieve an item by number. great for random item generation
     Dictionary<int, TestItem> itemDatabaseInt = new Dictionary<int, TestItem>();
 
     void Start()
     {
         CreateItemDatabase();
     }
 
     void CreateItemDatabase()
     {
         TestItem item;
 
         item = new TestConsumable();
         item.Name = "Health Potion";
         item.Icon = Resources.Load<Sprite>("Icons/" + item.Name) as Sprite;
         itemDatabase.Add(item.Name, item);
         itemDatabaseInt.Add(0, item);
 
         item = new TestConsumable();
         item.Name = "Mana Potion";
         item.Icon = Resources.Load<Sprite>("Icons/" + item.Name) as Sprite;
         itemDatabase.Add(item.Name, item);
         itemDatabaseInt.Add(1, item);
 
         item = new TestWeapon();
         item.Name = "Short Sword";
         item.Icon = Resources.Load<Sprite>("Icons/" + item.Name) as Sprite;
         itemDatabase.Add(item.Name, item);
         itemDatabaseInt.Add(2, item);
 
         item = new TestShield();
         item.Name = "Kite Shield";
         item.Icon = Resources.Load<Sprite>("Icons/" + item.Name) as Sprite;
         itemDatabase.Add(item.Name, item);
         itemDatabaseInt.Add(3, item);
 
     }
Comment
Add comment · Show 8
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 MercenarySed2 · Jun 01, 2016 at 08:50 PM 0
Share

The code works and gives me an item database that i can call by name or number just as i wanted. the problem is that, for example, TestWeapon has a public variable called atkType but I can't access it: item.atkType = AtkType.Sword; Every type on this screen inherits from TestItem

avatar image TBruce MercenarySed2 · Jun 01, 2016 at 09:14 PM 0
Share

To diagnose your problem you need to attach the TestItem script.

avatar image MercenarySed2 TBruce · Jun 02, 2016 at 12:16 AM 0
Share
 using UnityEngine;
 using System.Collections;
 
 public class TestBuffItem : TestItem {
 
     Hashtable buff;
 
     public TestBuffItem()
     {
         //initialize hashtable
         buff = new Hashtable();
     }
 
     public TestBuffItem(Hashtable ht)
     {
         buff = ht;
     }
 
     public void AddBuff(TestBaseStat stat, int amount)
     {
         buff.Add(stat.Name, amount);
     }
 
     //how many buffs on item
     public int BuffCount()
     {
         return buff.Count;
     }
 
     //getters and setters
     public Hashtable Buff
     {
         get { return buff; }
         set { buff = value; }
     }
 
 }
avatar image MercenarySed2 TBruce · Jun 02, 2016 at 12:17 AM 0
Share
 using UnityEngine;
 using System.Collections;
 
 public class TestWeapon : TestBuffItem {
 
     int _phyDmg;
     int _magDmg;
     //the combo differs depending on attack type
     AttackType _atkType;
     //if its 2 handed weapon u cnt equip a shield
     bool _isTwoHanded;
 
     public TestWeapon()
     {
         _phyDmg = 0;
         _magDmg = 0;
         _atkType = AttackType.Sword;
         _isTwoHanded = false;
     }
 
     #region getters and setters
     public int PhyDmg
     {
         get { return _phyDmg; }
         set { _phyDmg = value; }
     }
 
     public int $$anonymous$$agDmg
     {
         get { return _magDmg; }
         set { _magDmg = value; }
     }
 
     public AttackType AtkType
     {
         get { return _atkType; }
         set { _atkType = value; }
     }
 
     public bool IsTwoHanded
     {
         get { return _isTwoHanded; }
         set { _isTwoHanded = value; }
     }
     #endregion
 
 }
 
 public enum AttackType
 {
     Sword,
     Bow,
     Staff
 }
avatar image MercenarySed2 · Jun 02, 2016 at 12:15 AM 0
Share
 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class TestItem   {
     
     string _name;
     int _value;
     Sprite _icon;
 
     //some items just can't be removed from inventory
     bool _canBeRemoved;
 
     //private int _descr;
     //private int _lore;
 
     public TestItem()
     {
         _name = "Needs Name";
         _value = 0;
         _canBeRemoved = true;
         _icon = Resources.Load<Sprite>("Icons/Empty") as Sprite;
     }
 
     public TestItem(string name, int val, Sprite icon)
     {
         _name = name;
         _value = val;
         _canBeRemoved = true;
         _icon = icon;
     }
 
     #region   //getters and setters
     public string Name
     {
         get { return _name; }
         set { _name = value; }
     }
 
     public int Value
     {
         get { return _value; }
         set { _value = value; }
     }
 
     public Sprite Icon
     {
         get { return _icon; }
         set { _icon = value; }
     }
 
     public bool CanBeRemoved
     {
         get { return _canBeRemoved; }
         set { _canBeRemoved = value; }
     }
     #endregion
 }
avatar image TBruce MercenarySed2 · Jun 02, 2016 at 01:05 AM 0
Share

Before I can answer your question I need to know are you having the problem in CreateItemDatabase() or are you having the problem here

 _atkType = AttackType.Sword;

inside of TestWeapon()?

avatar image MercenarySed2 TBruce · Jun 02, 2016 at 01:57 AM 0
Share

the code works fine. i just want to assign the variable _atkType in the CreateItemDatabase() function itself. it creates the items and they are useable in my game but i can't access any variables other thn the TestItem variables. TestWeapon inherits from TestBuffItem which inherits from TestItem but only the TestItem variables are accessible. I can't even add buffs to my weapon or any other TestItem child(TestConsumable inherits TestBuffItem inherits TestItem)

avatar image MercenarySed2 TBruce · Jun 02, 2016 at 02:05 AM 0
Share

alt text

itemdatabase-error.png (63.5 kB)

1 Reply

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

Answer by TBruce · Jun 02, 2016 at 02:32 AM

Your problem is that item is your base class and even though this is valid

 TestItem item;
 item = new TestWeapon();

because TestWeapon is a descendant of TestItem, this is invalid

 item.atkType = AtkType.Sword;

because TestItem does not have a member atkType. What you can do however is this

 ((TestWeapon)item).atkType = AtkType.Sword;

or this

 // more preferred
 TestWeapon weapon = new TestWeapon();
 weapon.atkType = AtkType.Sword;
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 MercenarySed2 · Jun 02, 2016 at 02:46 AM 0
Share

haha sorry for wasting your time. i knew that but as you can see i have a huge rpg with lots of code and i wanted to not type so much. Was looking for a generic or shortcut but i guess i just gotta stop crying and do more typing lol. thanks for taking the time to follow up. its really appreciated

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

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

Related Questions

Make Parent Follow Child and Vice Versa? 1 Answer

Destroy one child (of same instance) after another? 0 Answers

How to set an object to be a child of another using c# 1 Answer

Moving grabbed object OnTriggerStay2D 0 Answers

Instantiating as a child of an object in hierarchy 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