Unity
Unity Scripte Başka Bir Scriptten Erişebilme ve Değişkerene Erişmek
Unity de başka bir scripttki değişkeni değiştirmek istiyoruz ama ikisini birbirine bağlayıp arada bağlantı kurmamız gerekiyor bunu nasıl yapacağımızı kısa bir yöntemle anlatıyorum. Kodumuz aşağıda bunu alıp erişilmesini istediğimiz scripte yapıştırıyoruz.
public static BuSCRIPTInADI instance;
private void Awake()
{
CreateInstance();
}
void CreateInstance()
{
if (instance == null) { instance = this; }
else { Destroy(gameObject); }
}
Şimdi diğer scrite girip aşağıdaki kod ile tüm değişkenlere erişebilir değiştirebiliriz :)
BuSCRIPTInADI.instance. noktadan sonra o scriptteki public olarak tanımladığımız değişkenler görünecektir.
private void Awake()
{
CreateInstance();
}
void CreateInstance()
{
if (instance == null) { instance = this; }
else { Destroy(gameObject); }
}
Örnek:
BuSCRIPTInADI.instance.can = 100;
BuSCRIPTInADI.instance.can = 100;
Yorum Gönder
0 Yorumlar