Search code examples
unity-game-enginecastinggameobject

Cast in unity 3d


This is a piece of Code I found on internet

public List<Piece> ramps = new List<Piece>();


1  Gameobject go;
2  go = ramps[visualIndex].gameObject

I want to know why the author has put ".gameObject" at the end of the line 2. Is it another type of cast? Thanks


Solution

  • Piece is most likely a custom component inheriting from MonoBehaviour.

    ramps[visualIndex] will retrieve a reference to the Piece component.

    go is declared as a GameObject, so calling ramps[visualIndex].gameObject will retrieve the gameObject the component is attached to.

    https://docs.unity3d.com/ScriptReference/Component-gameObject.html

    There is no cast at all involved here.