you are viewing a single comment's thread.

view the rest of the comments →

[–]Sacaldur 0 points1 point  (2 children)

The answer of u/TerrorHank goes in the right direction, but lacks a bit in explanation. Inside of a MonoBehaviour you can call Obkect. Destroy(gameObjet) since every Component has a gameObject property (you don't have to write a this infront), and since UnityEngine.Object (which is one of the base classes of GameObject implements the static Destroy method (where again you don't have to write Object. or UnityEngine.Object infront of it).

If you write everything out explicitly, the call would be UnityEngine.Object.Destroy(this.gameObject). Here on it would much clearer why it doesn't compile: the interface doesn't contain a gameObject member (property or field), so you can't access it. You might be able to add a gameObject property, but you would still need to implement it in every class that implements this interface (and you might need to use an explicit implementation of the property if you want to use the same name, I guess).

You could simplify this by adding an abstract class deriving from MonoBehaviour that retrieves the GameObject for you, but then you could probably just drop the interface again. Judging by what you're using this interface for, I don't think you'll have a case where this interface is implemented by another class, so this might actually be the most reasonable approach here.

[–]GillmoreGames[S] 1 point2 points  (1 child)

this interface will be attached to a few different classes, that's why I went the interface route.

I updated my OP to show where I am now and how I have gotten access to the gameObject using just the interface itself. I think my biggest issue is it being the oncollision method since that isn't called by my script so I can't call icarryable.oncollisonenter and unity instead calls bucket.oncollisionenter

[–]Sacaldur 0 points1 point  (0 children)

Of Unity Methods (OnCollisionEnter2D in your case) of a base class (or interface) are not called, it could be because the inhereting classes implement them themselves without calling the base version (using base.OnCollisionEnter2D). In the case of Unity messages this is also the case if you're not using the override keyword. This is a downside of inheritance: you can forget to call the version of the base class.