Problem with Weapon Spinning

I have a Script should be able to spin a weapon like in the old gta games but it keeps freezing unity. I’m using a model from 3ds max

var Speed : float;

function Update() {

for(var i = 0; i < 1;){

transform.Rotate(Vector3.right * Speed);

     }
}

Can anyone Help ?

sorry i’ve never played GTA, but do you want it to just rotate constantly? If this is all you want to do the easiest way would be (in my opinion) this script.

Javascript:

var RotateSpeed:float=1;//speed of rotation
var YisTop:boolean=false;
var XisTop:boolean=false;
var ZisTop:boolean=false;
// some objects have different tops, meaning their rotations will be different on the ground, so choose which one works for the weapon

function Update(){
if (YisTop){
transform.rotation=Quaternion.Euler(Vector3(0,Time.time*RotateSpeed,0));
} else if (XisTop){
transform.rotation=Quaternion.Euler(Vector3(Time.time*RotateSpeed,0,0));
} else {
transform.rotation=Quaternion.Euler(Vector3(0,0,Time.time*RotateSpeed));
}
}

C#: (after the public class is stated)

Float RotateSpeed=1;
Bool YisTop=false;
Bool XisTop=false;
Bool ZisTop=false;
// some objects have different tops, meaning their rotations will be different on the ground, so choose which one works for the weapon
Void Update(){
if (YisTop){
transform.rotation=Quaternion.Euler(Vector3(0,Time.time*RotateSpeed,0));
} else if (XisTop){
transform.rotation=Quaternion.Euler(Vector3(Time.time*RotateSpeed,0,0));
} else {
transform.rotation=Quaternion.Euler(Vector3(0,0,Time.time*RotateSpeed));
}
}

mind you these are not tested and their may be a few error codes. Hope this helped :lol:

Your FOR loop never terminates.

for ( x = 0; x < 1; ) {
}

The loop will exit when x >= 1, but you never set x to be >= 1.

you want
for ( var x : int = 0; x < 1; x++ ) {

However, that’s just doing something once, so you really want no for loop at all unless you plan on changing the numbers.

@Vincenti

I was thinking of doing something like

var i : int;
var Speed : float; 
var Player : GameObject;

function Update() { 

for( i = 0; i < 1;){ 

transform.Rotate(Vector3.right * Speed); 

     } 

function OnTriggerEnter() {

i = 1;

Player.transform.parent = transform;

     }
}

But you’ll still have a loop that never exits.
Without a yield or return, Unity gets inside the function and cannot escape, so the infinite-loop handler crashes Unity “more gracefully” rather than let it hang forever.

You need to store a state somewhere that enables/disables
spinning.

e.g.

private var spin : boolean = true;

function Update () {
  if ( spin )
    // spin;
}

function OnTriggerEnter( -- ) {
 if ( -- ) {
   spin = false;
 }
}

Totally, Vicenti. The update function never ends! In fact, there isn’t any reason to use a loop for the spinning. Each time ‘update’ gets call you only need to rotate a few degrees, and only once. ‘Update’ is already like an endless ‘for’ loop.