尝试编写代码,在代码运行时将游戏对象添加到列表中。没有错误,但是代码只是添加了一个空的游戏对象,即使已经确定了一个特定的游戏对象。
代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class HomeScreen : MonoBehaviour
{
public List<GameObject> myCollection;
public int money = 1000;
[SerializeField] public TMPro.TextMeshProUGUI currentMoney;
// Start is called before the first frame update
void Start()
{
myCollection.Add(Resources.Load<GameObject>("Assets/Resources/dogPrize.prefab"));
}
void Update()
{
currentMoney.text = "Money: $" + money.ToString();
}
public void buyScreen()
{
SceneManager.LoadScene (sceneName:"Buy Screen");
}
}
实际产量:
游戏对象所在位置:
预期结果: