私はshelf.csとGameManager.csのクラスを持っています。 shelf.csでは、displayAppleメソッドは文字列を返します。次に、GameManagerは文字列にアクセスし、それをテキストフィールドに表示します。テキスト表示のために他のクラスの変数にアクセスできません[Unity]
shelf.cs 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.
}
}
で、その後
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;
}
}
テキストフィールドには文句を言わない文字列を表示し、エラーがあります。
NullReferenceException: Object reference not set to an instance of an object
GameManager.Start() (at Assets/scripts/GameManager.cs:12)
"shelf"は "Shelf"でなければなりません。 – Fattie
それ以外は?何も間違っていない? – whoami
"displayApple"は "DisplayApple"でなければなりません – Fattie