copy instance of a base class to instance of inherited class

using UnityEngine;
using System.Collections;

public class CollectItem : MonoBehaviour
{
	public Item item = new Item();
	
	void OnTriggerEnter(Collider other)
	{
		if(other.tag == "Collectable")
		{
			item = other.GetComponent<ItemModifier>().newItem;
			if(item.Type == ItemType.keyItem)
			{
				this.item= new KeyItem();
				inventorySystem.keyItemList.Add(item as KeyItem);
			}
			else if(item.Type == ItemType.weaponItem)
			{
				this.item= new Weapon();
				inventorySystem.weaponList.Add(item as Weapon);
			}
			else
				inventorySystem.inventory.Add(item);
		//Debug.Log(item.ItemName+" / "+item.Description+" / "+item.Type);
			
		}
	}	
}

where my base class is Item and the inherited classes are KeyItem and Weapon.
if i do this i just get empty instances and i want it to take the values it has as Item to KeyItem or Weapon so i can access their fields…

Jister,

These two lines:
this.item = new KeyItem();
this.item = new Weapon();

are creating new KeyItem and Weapon objects, respectively. That’s what new does. Then you are using the parameter less constructor of each object, which is why the instances are empty.

I would do it with the is keyword like this:

...
item = other.GetComponent<ItemModifier>().newItem;
if(item is KeyItem)
     inventorySystem.keyItemList.Add(item as KeyItem);
else if (item is Weapon)
     inventorySystem.weaponList.Add(item as Weapon);
...

Now this doesn’t make a copy of the object. It just references the one from other. If you need to have it make a copy, then I would recommend you create constructors or methods that duplicate Key Item and Weapon.

k thanks that did it.
I moved the type check before creating the base newItem in ItemModifier

still is this how i suppose to use inheritance classes?
I have the base Item and for now 2 classes that inherit from Item: KeyItem and Weapon
then i have a ItemModifier script to make game objects items and an ItemCollect script for the player to collect items
with what you showed me i now do this in my ItemModifier script:

using UnityEngine;
using System.Collections;

public class ItemModifier : MonoBehaviour {

	public string itemName;
	public int cost;
	public string description;
	public ItemType type;
	
	public Item newItem = new Item();
	
	void Awake ()
	{
		newItem.Type=type;
		if(newItem.Type==ItemType.keyItem)
			newItem= new KeyItem();
		if(newItem.Type==ItemType.weaponItem)
			newItem=new Weapon();
		print(newItem);
		newItem.ItemName=itemName;
		newItem.Cost=cost;
		newItem.Description=description;
		
		gameObject.tag="Collectable";
	}
}

I’m trying to put together pieces of what i learned in C# videos to the right object oriented practice. But its still a lot grope in the dark.

Jister,

It just takes time. It’s probably easier to learn OOP outside of Unity, since the Unity engine introduces some additional quirks.

What is ItemType?

an enum with all the item types I want to make inheritance classes of Item from.

this is my Item class

[System.Serializable]
public class Item
{
	private string _itemName;
	private int _cost;
	private string _description;
	private ItemType _type;
	
	public Item()
	{
		_itemName = string.Empty;
		_cost = 0;
		_description = string.Empty;
		_type = ItemType.common;
	}
	
	public string ItemName
	{ 
		get { return _itemName; }
		set { _itemName = value; }
	}
	
	public int Cost
	{
		get { return _cost; }
		set { _cost = value; }
	}
	
	public string Description
	{
		get { return _description; }
		set { _description = value; }
	}
	public ItemType Type
	{
		get { return _type; }
		set { _type = value; }
	}
	
}

public enum ItemType
{
	common,
	keyItem,
	weaponItem,
	consumableItem,
	ore,
	smallItem,
	bigItem
}

but i have some other question if you don’t mind
how do these objects/classes like KeyItem relate to gameObjects? can or do I make a gameObject a type of KeyItem or is KeyItem not even a Type but just a class? Or should I ask for the ItemModifier on the GameObject and check the type of Item through there?
I have a public List I want to fill manually with GameObject that are KeyItems, but don’t really get how to do it because I don’t understand the above.
so many things i don’t get yet, but hey at least it’s still fun :wink:

Jister,

class defines a new kind of type. GameObject is a type defined by class. But GameObject isn’t a very interesting type, it’s really only a container to hold other objects. It only holds objects that are Components or derived from Components (Behaviors and MonoBehaviors). This is how we interact with GameObject in Unity, by writing a script that derives from MonoBehavior and attaching it to a GameObject object.

If you just want to get all KeyItems at the start of your game. Then you could probably do something like:

List<KeyItem> listKeyItem = new List<KeyItem>();
...
foreach(KeyItem kItem in GetComponents<KeyItem>())
{
    listKeyItem.Add(kitem);
}

C

thanks CahMan
so a class is a Type :wink:
I want to set the list manualy for my puzzleModifier script, so i can setup a list of object the player has to go and find for this puzzle. but puzzles should only look at KeyItem so i wannted to have a list that only takes keyitems.

one last Q

in ItemModifier i do:

void Awake ()
	{
		newItem.Type=type;
		if(newItem.Type==ItemType.keyItem)
		{
			newItem= new KeyItem();
		}
print (newItem);
}

where the print result is KeyItem.

but if i want to do

void Awake ()
	{
		newItem.Type=type;
		if(newItem.Type==ItemType.keyItem)
		{
			newItem= new KeyItem();

                        newItem.ItemPosition = gameObject.transform.position;
		}

it doesn’t recognize the .ItemPosition although i have in my KeyItem class

using UnityEngine;
[System.Serializable]
public class KeyItem : Item 
{
	private Vector3 _itemPosition;
	
	public KeyItem ()
	{
		_itemPosition = Vector3.zero;
	}
	
	public Vector3 ItemPosition
	{
		get{return _itemPosition;}
		set{_itemPosition = value;}
	}
}

so i suspect that newItem isn’t a KeyItem after: newItem = KeyItem();
confusseling to say the least…?

Jister,

in the ItemModifier script, you have newItem defined as an Item type:

Item newItem = new Item();

then you set newItem

newItem = new KeyItem();

newItem is still an Item type, however. So it can only access fields, properties, and members of the Item type. If you want to treat it as a KeyItem, then you have to put it in a KeyItem

KeyItem keyItem = new KeyItem();
keyItem.ItemPosition = gameObject.transform.position;

C

ah ok but then back to my original question :wink: but maybe convert is a better word for it.
so how do i convert a Item to a KeyItem?
something like:

 Item newItem = new Item();
KeyItem newKeyItem = new KeyItem();
newItem = newKeyItem
newKeyItem.ItemPosition = gameObject.transform.position;

or maybe i should just move ItemPosition to the Item class…
begining to think I’m going the wrong way about my logic for an inventory and puzzle system.

edit:
ah how stupid of me i shouldn’t be using item, its just for inheritance i should use KeyItem an Weapon in my ItemModifier.

anway thx for the patience C.