【问题标题】:Motion of prefabs on the circle with same starting position but different speed [duplicate]预制件在具有相同起始位置但不同速度的圆上的运动[重复]
【发布时间】:2021-01-28 19:09:09
【问题描述】:

有一段代码可以将 GameObject 实例化到列表中

public GameObject prefab = null;

void Start()
{
  List<GameObject> _list = new List<GameObject>();
  for (int i = 0; i < 4; i++)
  {
    GameObject thisObject = Instantiate(prefab) as GameObject;
    _list.Add(thisObject);
  }
}

我想使用给定列表中的预制件并按如下方式使用它(在圆上运动,起始位置相同但速度不同):

void Update()
{
 for (int i = 0; i < 4; i++)
     {
      _list[i].transform.position = new Vector3(Mathf.Cos(i * Time.deltaTime),Mathf.Sin(i * Time.deltaTime),0);
     }
 }

但有错误消息:“名称'_list'在当前上下文中不存在”

【问题讨论】:

    标签: unity3d


    【解决方案1】:

    _list 是 Start 方法中的局部变量。 你必须声明一个成员。

    public GameObject prefab = null;
    public readonly List<GameObject> _list = new List<GameObject>();
    
    void Start()
    {
      _list.Clear();
      for (int i = 0; i < 4; i++)
      {
        GameObject thisObject = Instantiate(prefab) as GameObject;
        _list.Add(thisObject);
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-13
      • 1970-01-01
      • 1970-01-01
      • 2013-08-10
      • 2019-03-05
      • 1970-01-01
      • 2021-12-06
      • 1970-01-01
      相关资源
      最近更新 更多