2017-04-13 68 views
-3

這是代碼。我想在Update方法中使用SpwanArrow方法的int randomIndex值。有任何想法嗎?從一種方法獲取int值到另一個C#

void SpawnArrow() 
{ 
    //some code here   
    int randomIndex = UnityEngine.Random.Range(0, arrows.Length); 
    GameObject prefab = arrows[randomIndex]; 
    GameObject clone = Instantiate(prefab, new Vector3(0.02F, 2.18F, -1), 
    Quaternion.identity); 
} 

void Update() 
{ 
    if (randomIndex == 1 && (Input.GetButtonDown("a") == true)) 
    { 
     ScoreManager.score += scoreValue; 
    } 
    //and so on 

} 
+0

使它成爲一個一流水平的變量。 – juharr

+1

你想用'randomIndex'做什麼? – Romoku

回答

0

使它成爲一個全局變量在你的類

int randomIndex = -1; 

void SpawnArrow() 
{ 
    //some code here   
    randomIndex = UnityEngine.Random.Range(0, arrows.Length); 
    GameObject prefab = arrows[randomIndex]; 
    GameObject clone = Instantiate(prefab, new Vector3(0.02F, 2.18F, -1), 
    Quaternion.identity); 
} 

void Update() 
{ 
    if (randomIndex == 1 && (Input.GetButtonDown("a") == true)) 
    { 
     ScoreManager.score += scoreValue; 
    } 
    //and so on 

} 

我已經將它設置爲-1,因爲它沒有影響到你的更新功能。

0

您可以使randomIndex成爲一個私有屬性。這將允許您在課堂的任何地方使用它。

private int randomIndex { get; set; } 

void SpawnArrow() 
{ 
    //some code here   
    randomIndex = UnityEngine.Random.Range(0, arrows.Length); 
    GameObject prefab = arrows[randomIndex]; 
    GameObject clone = Instantiate(prefab, new Vector3(0.02F, 2.18F, -1), Quaternion.identity); 
} 

void Update() 
{ 
    if (randomIndex == 1 && (Input.GetButtonDown("a") == true)) 
     ScoreManager.score += scoreValue; 
    //and so on 
} 
0
int randomIndex; 

void SpawnArrow() 
    { 
     //some code here   
     randomIndex = UnityEngine.Random.Range(0, arrows.Length); 
     GameObject prefab = arrows[randomIndex]; 
     GameObject clone = Instantiate(prefab, new Vector3(0.02F, 2.18F, -1), 
     Quaternion.identity); 
    } 

    void Update() 
    { 
     if (randomIndex == 1 && (Input.GetButtonDown("a") == true)) 
     { 
      ScoreManager.score += scoreValue; 
     } 
     //and so on 

    } 
相關問題