- Home /
Getting parameters of one interface in another interfaces object
Ok, I'm sorry if you have trouble understanding me, my technical english isn't very good. However...
I have 2 interfaces.
public interface IItem
{
GameObject itemGO { get; set; }
}
public interface IHasItemPlace {
GameObject itemPlace { get; set; }
IItem item { get; set; }
bool hasItemPlaced { get; set; }
}
As you can see, IHasItemPlace has a parameter of IItem. There is a class Player, that implements IHasItemPlace, and there is also a class Salver, that implements both. The thing is, when i equate item in Player to a Salver object, i can't access it's IHasItemPlace parameters. I need to get an item, that is in salver, while the salver itself is in player. I thought to do something like
(IHasItemPlace)player.item.item
or
player.(IHasItemPlace).item.item
But it doesn't seem to work (of course). Is there any way to make this happen?
Answer by NoseKills · Oct 20, 2016 at 06:05 AM
You have to test whether the current item is actually an IHasItemPlace because not all items are.
IHasItemPlace itemHolder = player.item as IHasItemPlace;
if (itemHolder != null) {
itemHolder.item...
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How do I get gameobjects to fire a method based on an interface? 1 Answer
Annoying MissingReferenceException with Interfaces and Monobehavior 1 Answer
Find all scripts attached to an object that implement a certain interface 1 Answer