Ich habe die Klasse von shelf.cs und GameManager.cs. In der Datei shelf.cs gibt die Methode displayApple eine Zeichenfolge zurück. Dann greift der GameManager auf die Zeichenfolge zu und zeigt sie im Textfeld an.Kann nicht auf Variable in einer anderen Klasse für die Textanzeige zugreifen [Unity]
shelf.cs
public class shelf: MonoBehaviour
{
//adding all the gameobjects into a list
public List<GameObject> players;
public string textApp ="";
public string displayApple()
{
for (int i = players.Count - 1; i >= 0; i--) {
if (players [i].name == "Apple") {
textApp = textApp + players [i].GetComponent<Apple>().type + " " + players [i].GetComponent<Apple>().colour + " " + players [i].GetComponent<Apple>().weight;
textApp = textApp + "\n";
}
}
long_apple= textApp;
return long_apple;
}
}
dann in GameManager.cs
public class GameManager : MonoBehaviour {
Basket bask;
public Text text_apple;
// Use this for initialization
void Start() {
text_apple.text = bask.displayApple; //i want to call the method of displayApple to get the string returned.
}
}
Das Textfeld wird nicht die Zeichenfolge angezeigt werden und es gibt Fehler.
NullReferenceException: Object reference not set to an instance of an object
GameManager.Start() (at Assets/scripts/GameManager.cs:12)
"Regal" muss "Regal" – Fattie
anderen als das sein? nichts ist falsch? – whoami
"displayApple" muss "DisplayApple" sein – Fattie