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);
 
     }
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
To diagnose your problem you need to attach the TestItem script.
 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; }
     }
 
 }
 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
 }
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()?
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)
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;
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
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                