2014-03-25 99 views
0

列表,並隨機選擇項目我有一個遊戲loginc以及其中用於MyCity一個公共類定義MyCity.cs一個統一的腳本PlayerController.cs。填充在Unity

我的目標是在PlayerController.cs中填充列表。

該列表包含城市及其x,y,z Vector3座標。

我的PlayerController腳本應隨機選擇一個城市我的名單和使用,在我的SetTargetCity功能,所以用適當的Vector3座標它可以創建一個新的遊戲對象。

我收到此錯誤:

「這個名字mycities」不當前文檔」

我在做什麼錯在存在嗎?創建mycities不會做的伎倆一個公共變種....

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

剛在Start方法的外面定義myCities,並將其初始化爲:

List<MyCity> mycities; 
void Start() 
{ 
    mycities = new List<MyCity>(); 
    ... 
} 
+0

That di訣竅!非常感謝! – Killbert

+0

我現在需要做的是爲每個城市找到適當的Vector3座標....我將場景中的對象放置在平面上方的正確位置......在Unity檢查器中,這些對象在其Transform屬性中具有XYZ座標....但這些顯然不同於我場景中所需的Vector3座標......任何想法? – Killbert

+0

忽略最後的評論,得到正確的Vector。我的對象與世界沒有關係。 – Killbert