IndexOutOfRangeException error when getting transform of array object
I'm trying to instantiate a new game object and make its position equal to the position of an object in an array, however whenever I play the game and instantiate the object I get the indexoutofrangeexception error. Anybody know how to fix it?
while(currentLeftCannon <= numberOfLeftCannons)
{
GameObject newCannonball = Instantiate(cannonball, leftCannons[currentLeftCannon], true) as GameObject;
Rigidbody cannonballRigidbody = newCannonball.GetComponent<Rigidbody>();
cannonballRigidbody.AddForce(Vector3.left * cannonballSpeed * Time.deltaTime, ForceMode.Impulse);
Destroy(newCannonball, 5f);
currentLeftCannon++;
}
if (currentLeftCannon > numberOfLeftCannons)
currentLeftCannon = 0;
If you receive this error, currentLeftCannon is larger or equal to the size of leftCannons. $$anonymous$$ake sure it's within the range of the array's length.
Your answer
Follow this Question
Related Questions
How to instantiate bullets in multiple transforms 1 Answer
I got Some Error about how to Transform Postiton Enemy where one point can spawn 2 enemies. 1 Answer
What's the best way to rotate an object relative to another? 0 Answers
Give instantiated object same transform as localPlayer. 1 Answer
Checking if an instance of a Script saved in 3d array is null 1 Answer