Inheritence problems with raycast
I am making an inventory system. So i made an inheritence script calles Items, and i have object that inherit from that. I made different scripts; cubes, spheres and capsules. I have a raycast on click, and then i tell my raycast to look for the item hit. I made a function in the Item base script and then override it in the child scripts. But it always sees it as a cube no matter what i do. I have a cube with the script cubes on it and a capsule with Capsules.
This is my item script;
public virtual GameObject IHaveItem(GameObject currentItem)
{
print("base class enter");
return currentItem;
}
and this is my child inheritence:
public GameObject cubeTex;
public override GameObject IHaveItem(GameObject currentItem)
{
print("cube override");
currentItem = cubeTex;
return currentItem;
}
i do the same for the other two (spheres and capsules)
This is where i look which item i am clicking on:
public void WhichItem()
{
if (Physics.Raycast(transform.position, transform.forward, out hit, 50.0f))
{
Debug.DrawRay(transform.position, transform.forward, Color.red, 50.0f);
if (hit.collider.GetComponent<Items>())
{
print("hit an object");
items.IHaveItem(currentItem);
print("current item 1 =" + currentItem);
currentItem = items.IHaveItem(currentItem);
print("current item 2 =" + currentItem);
LookForSpace();
hitItemSC = true;
}
}
}
Can someone please help me? i tried a lot of stuff and i really cant figure this one out. If you dont know but have an idea thats good to, im willing to try them. i just dont wanna lose my inheritence part
@CharlieLama - IHaveItems are just your methods.... not full classes, which would have been helpful to see. Also, calling IHaveItem will return a GameObject.
@CharlieLama
Your script logic feels somewhat unclear, why are you calling your single item "Items" ins$$anonymous$$d of "Item"?
Also, why each item has IHaveItem method that receives some GameObject? In your WhichItem method, it does not make any sense, at least to me, what you are doing. You first raycast, then if your hit has Items component, you pass some "currentItem" to your item? What is this current item and why are you passing it? You definitely don't need to pass your item anything to see what it is, you should be checking the type of hit object.
Yes, iHaveItems is only a method but also my full class, there is nothing else in there
because Items is an inherit class and cubes inherit from Items, and capsules and Spheres too Yes it is because i am making an inventory system, i want to have the item i just hit, so i can spawn a texture linked to my hit object
Answer by CharlieLama · Oct 15, 2018 at 03:04 PM
Thanks to @eses i realised a silly issue
Instead of currentItem = items.IHaveItem(currentItem);
i used currentItem = hit.transform.gameObject;
seemed to fix everything for me