2017-02-02 6 views
-2

anerkannt in Ich habe folgende C# Skript einige Partikelsystem Physik in der Einheit für die Verwaltung:Globale Variable nicht For-Schleife

//PS Variables 
ParticleSystem myPS; 
public List<ParticleCollisionEvent> collisisonEvents; 

//Physics variables 
public float effect;  


// Use this for initialization 
void Start() { 
    myPS = GetComponent<ParticleSystem>(); 
    collisisonEvents = new List<ParticleCollisionEvent>(); 
} 


void OnParticleCollision (GameObject other) 
{ 
    //Checking if the hit object is indeed the ball 
    bool isBall = other.tag.Equals("Player"); 
    if (isBall) 
    { 
     Rigidbody2D hitObject = other.GetComponent<Rigidbody2D>(); 

     //Getting the number of collisions that have occured this frame 
     int numOfCollisions = myPS.GetCollisionEvents(other, collisisonEvents); 
     Vector3 particleDirection = new Vector2(0,0); 

     //Iterating through all the events 
     for (int i = 0; i < numOfCollisions; i++) 
     { 
      //Calculating a resultant direction 
      particleDirection += collisionEvents[i].velocity; 
     } 

     //Applying the resultant force 
     hitObject.AddForce(particleDirection.normalized * effect * numOfCollisions); 
    } 
} 

Es wenn ein Problem mit dem Umfang der collisionsEvents Liste zu sein scheint, als Ich kann es nicht in der For-Schleife in der OnParticleCollision Co-Routine verwenden. Ich bekomme weiterhin den Fehler, dass "collisionsEvents in diesem aktuellen Kontext nicht existiert." Die Variable myPS hat dieses Problem jedoch nicht und wurde an derselben Stelle wie collisionsEvents deklariert.

Könnte mir bitte jemand hier helfen?

+2

collisionEvents = collisisonEvents! – Gusman

Antwort

3

Sie haben den Variablennamen falsch geschrieben. In der Deklaration verwenden Sie collisisonEvents und in der Methode collisionEvents. Das Problem ist nicht der Umfang. Wenn Sie es auf Klassenebene deklarieren, wird der Compiler nur die Nichtvorhandene beschweren, wenn Sie einen Tippfehler machen.

Obwohl ich zugeben muss, dass solche Dinge sind manchmal hart wie die Hölle zu erkennen;)