在Unity中填充列表和随机选择项

时间:2014-03-25 11:38:26

标签: c# list unity3d

我有一个带有游戏loginc和MyCity.cs的Unity脚本PlayerController.cs,其中定义了MyCity的公共类。

我的目标是在PlayerController.cs中填充List。

该列表包含城市及其x,y,z Vector3坐标。

我的PlayerController脚本应该从我的列表中随机选择一个城市并在我的SetTargetCity函数中使用它,这样它就可以用适当的Vector3坐标创建一个新的游戏对象。

我收到此错误:

'mycities名称'在当前文档'

中不存在

我做错了什么?为mycities创建一个公共var并不能解决问题......

MyCity.cs包含以下内容:

using UnityEngine;
using System.Collections;

public class MyCity
{
public string name;
public float xcor;
public float zcor;
public float ycor;

public MyCity(string newName, float newXcor, float newZcor, float newYcor)
{
    name = newName;
    xcor = newXcor;
    zcor = newZcor;
    ycor = newYcor;
}
 }

然后PlayerController脚本如下所示:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class PlayerController : MonoBehaviour 
{
public float speed;
public float smooth = 2.0F;
public GUIText countText;
public GUIText targetCity;
private int count;
public GameObject cityPrefab;


void Start()
{


    List<MyCity> mycities = new List<MyCity>();

    mycities.Add( new MyCity("Maastricht", -5F, 3F, -1F )); 
    mycities.Add( new MyCity("Breda", -6F, 3F, -2F));
    mycities.Add( new MyCity("Amsterdam", -2F, 3F, 4F));


//WHAT ELSE DO I NEED  TO DO TO THE ABOVE LIST SO THAT
//THE BELOW FUNCTION void SetTargetCity () WILL WORK? 

    // scoring points & display on screen (works)
    count = 0;
    SetCountText ();

}

// Player Movement (works)
void FixedUpdate ()
{
    float moveHorizontal = Input.GetAxis("Horizontal");
    float moveVertical = Input.GetAxis("Vertical");

    Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);

    Vector3 moveDirection= new Vector3 (moveHorizontal, 0, moveVertical);  
    if (moveDirection != Vector3.zero){
        Quaternion newRotation = Quaternion.LookRotation(moveDirection * -1);
        transform.rotation = Quaternion.Slerp(transform.rotation, newRotation,  Time.deltaTime * smooth);

        rigidbody.AddForce(movement * speed * Time.deltaTime);

    }

}
    // Score points by flying into city game object (works), switch off that target city game object (works), get new target city...(no idea)
void OnTriggerEnter(Collider other)
{
            if (other.gameObject.tag == "City") {
                    other.gameObject.SetActive (false); 

        count = count + 1;

        SetCountText ();
        SetTargetCity ();
            }
}

void SetCountText ()
{
    countText.text = "Passengers Picked up: " + count.ToString();
}




   // BELOW IS WHERE THINGS GO WRONG.

    void SetTargetCity ()
{
    var randomCity = mycities[0];
targetCity.text = "Fly to: " + randomCity.name.ToString();

GameObject instancedCity=(GameObject)GameObject.Instantiate(cityPrefab);
instancedCity.transform.position=new Vector3(randomCity.xcor,randomCity.ycor,randomCity.zcor);
}



    }

1 个答案:

答案 0 :(得分:1)

只需在myCities方法之外定义Start,然后将其初始化为:

List<MyCity> mycities;
void Start()
{
   mycities = new List<MyCity>();
   ...
}
相关问题