2016-05-01 28 views
0

Das folgende Beispiel funktioniert gut, wird jedoch außerhalb der Grenzen des Fehlerindexes angegeben. Weiß jemand wie ich das beheben kann?Index außerhalb der Grenzen Fehler Einheit C#

ein anderes von meinem Problem, und ich kann das Objekt auf der Berührungsposition nicht initialisieren. danke

void Update() 
    { 
     goldDisplay.text = "Gold " + gold; 

    if (Input.GetKeyDown(KeyCode.Space) || Input.GetTouch(0).phase == TouchPhase.Began) { 
     int rarityRoll = UnityEngine.Random.Range (0, 100); 
     if (rarityRoll < 99) { 
      // choose something from common 
      int roll = UnityEngine.Random.Range (0, commons.Length); 
      // instantiate 
      Instantiate (commons [roll].componentObjc, new Vector3(0,0,1), transform.rotation); 
      } else { 
      int roll = UnityEngine.Random.Range (0, rares.Length); 
      // instantiate 
      Instantiate (rares [roll].componentObjc, new Vector3(0,0,1), transform.rotation); 
     } 
    } 
} 
+0

die Leitungsfehler bekommen? –

+0

was ist rares [roll] .componentObjc? Sie programmieren einen Klassenoperator? oder Sie möchten rares schreiben [roll] .gameObject –

+0

die Zeile des Fehlers ist, dass ... if (Input.GetKeyDown (KeyCode.Space) || Input.GetTouch (0) .phase == TouchPhase.Began) { –

Antwort

0

der Input.GetTouch(0).phase Fehler haben Sie Input.touches[0].phase ersetzen Schlag Code, um Ihren Code verwenden sollten! void Update() { goldDisplay.text = "Gold" + Gold;

if (Input.GetKeyDown(KeyCode.Space) || Input.touches[0].phase == TouchPhase.Began) { 
    int rarityRoll = UnityEngine.Random.Range (0, 100); 
    if (rarityRoll < 99) { 
     // choose something from common 
     int roll = UnityEngine.Random.Range (0, commons.Length); 
     // instantiate 
     Instantiate (commons [roll].componentObjc, new Vector3(0,0,1), transform.rotation); 
     } else { 
     int roll = UnityEngine.Random.Range (0, rares.Length); 
     // instantiate 
     Instantiate (rares [roll].componentObjc, new Vector3(0,0,1), transform.rotation); 
    } 
} 

}

+0

Ich tat Änderung. Es funktioniert genauso wie zuvor. aber jetzt von einem anderen Fehlercode in derselben Zeile. "Array-Index ist außerhalb des Bereichs" –

1

Fix. Es funktioniert ohne Fehler

void Update() 
    { 

    goldDisplay.text = "Gold " + gold; 

    for (int i = 0; i < Input.touchCount; i++) 
    { 
     Touch touch = Input.GetTouch(i); 

     if (touch.phase == TouchPhase.Ended && touch.tapCount == 1) 
     { 
      Vector3 position = Camera.main.ScreenToWorldPoint(touch.position); 
      int rarityRoll = UnityEngine.Random.Range (0, 100); 
      if (rarityRoll < 99) { 
       // choose something from common 
       int roll = UnityEngine.Random.Range (0, commons.Length); 
       // instantiate 
       Instantiate (commons [roll].componentObjc, new Vector3 (0, 0, 1), transform.rotation); 
      } else { 
       int roll = UnityEngine.Random.Range (0, rares.Length); 
       // instantiate 
       Instantiate (rares [roll].componentObjc, new Vector3 (0, 0, 1), transform.rotation); 
      } 
     } 
    } 
} 

tks

+0

das ist ein guter Weg, um mit Berührungen Spaß zu haben –