Bir nesnenin componentlerine ulaşmak için kullanılır.Bu nesne dışarıdan bir nesne olabileceği gibi scriptin yazılmış olduğu nesne içinde kullanılabilir.Hatta bu komutun bazı varyasyonları ile kendi alt nesnelerinin componentlerine de ulaşabilirler.
Kod:
public class UsingOtherComponents : MonoBehaviour
{
public GameObject otherGameObject;
private AnotherScript anotherScript;
private YetAnotherScript yetAnotherScript;
private BoxCollider boxCol;
**** Awake ()
{
anotherScript = GetComponent<AnotherScript>();
yetAnotherScript = otherGameObject.GetComponent<YetAnotherScript>();
boxCol = otherGameObject.GetComponent<BoxCollider>();
}
**** Start ()
{
boxCol.size = new Vector3(3,3,3);
Debug.Log("The player's score is " + anotherScript.playerScore);
Debug.Log("The player has died " + yetAnotherScript.numberOfPlayerDeaths + " times");
}
}
Kod:
public class AnotherScript : MonoBehaviour
{
public int playerScore = 9001;
}
Kod:
public class YetAnotherScript : MonoBehaviour
{
public int numberOfPlayerDeaths = 3;
}