2017-05-29 39 views
-3

當他們得到正確答案時,我不知道如何增加numberquestion。 如果您有能力,請告訴我如何在每次提供正確答案時增加numberquestion1,以便知道何時轉到下一個問題。如何增加靜態int數?

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

注:我使用的是統一的遊戲引擎^^^^^

public class TextControl : MonoBehaviour { 

List<string> questions = new List<string>() {"When does time fly?","When does time?","When does?"}; 

List<string> CorrectAnswer = new List<string>() {"3","3","3"}; 

public static string SelectedAnswer; 

public static string ChoiceSelected="n"; 

public static int numberquestion=0; 

// Use this for initialization 
void Start() { 
    GetComponent<TextMesh>().text = questions [numberquestion]; 
} 

// Update is called once per frame 
void Update() 
{ 
    if (ChoiceSelected == "y") 
    { 


     if (CorrectAnswer [numberquestion] == SelectedAnswer) 
     { 
      Debug.Log("Correct"); 
      numberquestion++; 
      } 
     } 
    } 
} 

這是部numberquestion++,我需要改變,使其由一個增加。

+0

的代碼是正確的...... – Gusman

+0

看起來像功課 – hardkoded

+1

什麼是你的問題? –

回答

0

利用非靜態變量在實例化時保存當前計數。這給一試:

public class TextControl : MonoBehaviour { 

    List<string> questions = new List<string>() {"When does time fly?","When does time?","When does?"}; 

    List<string> CorrectAnswer = new List<string>() {"3","3","3"}; 

    public static string SelectedAnswer; 

    public static string ChoiceSelected="n"; 

    public static int numberquestion=0; 

    public int myNumberQuestion; 

    // Use this for initialization 
    void Start() { 
    GetComponent<TextMesh>().text = questions [numberquestion]; 
    } 

    // Update is called once per frame 
    void Update() 
    { 
     if (ChoiceSelected == "y") 
    { 


    if (CorrectAnswer [numberquestion] == SelectedAnswer) 
    { 
     Debug.Log("Correct"); 
     myNumberQuestion = ++numberquestion; 
     } 
    } 
} 

}