Hello!
I have a script that constantly fires an array of 4 GameObjects each with their own collider and rigidbody from a position every set amount of time. (Limited to four instead of instantiating infinite GameObjects for performance purposes)
In my Unity editor the shots/flying GameObjects are consistent and always one after the other at the correct rate.
However sometimes when I play the game on my device it shoots them erratically - sometimes much quicker but not at the same time interval between shots. Restarting the level sometimes fixes the problem so that they shoot at the correct rate but this doesn’t always help. Closing the application and restarting it also sometimes fixes it.
My code to fire the projectiles is this;
var sodBullets : Rigidbody[];
var bulletIndex = 0;
var bulletForce : float;
var shootInterval : float = 2;
var offset : float;
var nextShot : float = 0;
function Update()
{
if (Time.time >= nextShot + offset)
{
sodBullets[bulletIndex].rigidbody.velocity = transform.TransformDirection(Vector3(0,0,bulletForce));
nextShot += shootInterval;
bulletIndex++;
if (bulletIndex >= sodBullets.length)
{
bulletIndex = 0;
}
}
}
Does anyone know why it may be firing at a strange rate but only on the device - never in the editor? Thanks.
(Also if anyone knows how I could optimise this script perhaps through caching variables that would be helpful to know too seeing as there are around 20 GameObjects that this script is applied to and therefore 80 bullets flying around the place)
Unity Forums thread on same issue: http://forum.unity3d.com/threads/96997-Different-behaviour-on-device-(possibly-Time.time-related-)?p=632344#post632344