strange error , sometimes script works, sometimes not.

Hello unity forum!

i am having some troubles with a bug that doesn´t make any sense to me, sometimes the script get´s executed, and sometimes not, i have already tested everything and i can´t seem to “force” the bug, it just happens sometimes.

i have changed the Fixed Timestep from 0.03 to 0.05 , does this have anything to do with it? i am asking because i already had problems with this Timestep.

if this is needed. here is the Script :

var Car : GameObject;
static var timeofSpawn : int = 1;
var timestartspawn : int = 2;


var position : float;

InvokeRepeating("Spawn", timestartspawn, timeofSpawn);

function Start () {

	var position = Random.Range(-5,5);

}

function Spawn () {

	if(playerControler.died == false){
		if(PauseButton.Pause == false){
		
			Instantiate( Car, Vector3( position/5, 2.223612,0), transform.rotation);
			position = Random.Range(-5,5);
		}
	}

}

Move InvokeRepeating() into your Start() function. You cannot call methods like this, they somehow need to be connected to the main Unity functions (they can be called from other functions of course as well, but those are somehow called from the main functions as well).

Also, Random.Range(-5, 5) will return integers from -5 to 4. Probably you want to use floats instead. (Also please use code tags when posting code, not quote tags.)

–Eric

ty, i´ll try it out!

ty, i was not able to use floats on Random.Range, it did not acepted, do you know any reference about this i could use?

btw, ty guys, i got it to work, the problem was a whole other, i forgot to reset some important variables upon player death, still , thank you for the help!