The script is working but why when I try to change stuff in inspector the game is freezing ?

Everything is working when I’m running the game but once I put the mouse cursor on the variable timeToSpin field box in the inspector before I even can change the value there the whole game is freezing and I need to shut it down with task manager.

Something the script make it but not sure what and why and how to fix it.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MissleLauncherRotation : MonoBehaviour
{
    public Transform body;
    public Transform guns;
    public Vector3 spinAxisBody;
    public Vector3 spinAxisGuns;
    public float timeToSpin = 5f;

    public bool randomSpin = false;

    // Yes, if you make Start return IEnumerator it is automatically run as a 
    // Coroutine by Unity without the need for extra code
    IEnumerator Start()
    {
        while (true)
        {
            // This is the more convenient way of checking for a bool == true
            if (randomSpin)
            {
                spinAxisBody = new Vector3(0, Random.Range(-180, 180), 0);

                spinAxisGuns = new Vector3(Random.Range(-40, 0), 0, 0);
            }
            else
            {
                // whatever you want to happen otherwise
            }

            // Pre cache/calculate the start and end rotations 
            // Rotate per default uses the LOCAL space so we will do the same and use localRotation
            // But instead of adding to the existing rotation we will use it as a complete new rotation
            var startBodyRotation = body.localRotation;
            var targetBodyRotation = Quaternion.Euler(spinAxisBody);

            var startGunsRotation = guns.localRotation;
            var targetGunsRotation = Quaternion.Euler(spinAxisGuns);

            // Basically same as the while loop but I prefer to use a for loop so I can't forget to increase the timer
            // In my opinion this is simply better readable
            for (float spinTimer = 0; spinTimer < timeToSpin; spinTimer += Time.deltaTime)
            {
                // Factor linear moving from 0 to 1 within timeToSpin seconds
                var factor = spinTimer / timeToSpin;
                // optional add ease-in and -out
                //factor = Mathf.SmoothStep(0, 1, factor);

                // Interpolate linear (or with ease-in and -out) between the pre cached start and end rotations
                body.transform.localRotation = Quaternion.Lerp(startBodyRotation, targetBodyRotation, factor);
                guns.localRotation = Quaternion.Lerp(startGunsRotation, targetGunsRotation, factor);

                yield return null;
            }

            // Just to be sure to end up with clean values
            body.transform.localRotation = targetBodyRotation;
            guns.localRotation = targetGunsRotation;
        }
    }
}

Well, this logically happens when you drag your “timeToSpin” below 0. In that case your nested for loop will never run and therefore there is no yield statement that is executed. Therefore you get an actual infinite loop that is never interrupted.

You can fix this in various ways. One would be to add another yield inside the while loop at the end. That will ensure you’re still yielding the execution when the inner for loop does not run.

The second solution is to make sure your “timeToSpin” variable is never 0 or less. This should be done right before you start the for loop. You could add this line before your for loop:

timeToSpin = Mathf.Max(0.01f, timeToSpin);

This will ensure a minimum value of 0.01