Achie1
Achie1

Reputation: 387

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

Upvotes: 0

Views: 236

Answers (2)

Hellium
Hellium

Reputation: 7346

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.

Upvotes: 1

Branden
Branden

Reputation: 347

Gameobject go; This variable is declared as a GameObject type. You can grab the GameObject associated with any type of script variable. In this case the Piece variable, ramps[visualIndex].

Upvotes: 1

Related Questions