why is every 2. frame is weird with Time.deltaTime

ok is this normal if unity 3D is longer time active (not play) when I click Play my graphic get’s weird 1. frame stays always at first position and every 2. frame is the correct updated frame

at first I thought I have some leak at all new operators in every frame (creating 1000 new and not deleting them in end) because the bigger that my code got more often I’m getting this

but whenever I restart unity every frame is correctly updated

and today I’ve just opened unity and was so tired of everything from happening and went to bed and when I came back to try few stuff when I clicked play first time on run I got that weirdness again

ok I’ve just made an crash test and it does crash as usually I get it but usually it takes a day before unity crashes

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

public class UnityCrashTest : MonoBehaviour {
	public Rect crash ;
	
	void OnGUI(){
		for (int i = 0; i < 1000; i++) {
			GUI.Button(new Rect (i+80,i+80,50,50),"asdf","Box");
		}
	}
}

in a minute or 2 the graphics starts jumping but not as often if I have unity longer time running

as around every 2 sec I get jump to original position

but I was watching CPU and Memory and saw that memory was slooowly going up but at 10% memory usage unity couldn’t handle any longer and crashed

interesting thing is tho memory didn’t go back down (and if I start unity it’s like at beginning)

but if I restart PC memory goes back down

unity with this script crashes in around 30 min

sadly I cannot look in to GPU and Graphical memory

so how do I get rid of all new operators or how do I kill/destroy/delete all this new operators? after every frame so I won’t have leak

and after doing this if I restart unity around 5 times (with crash) I don’t even need to click play any longer and it just crashes in few min I can still click on windows and stuff… but not inside unity like - project play button scripts … …

and if I do it this way:

	public Rect crash = new Rect (0,0,80,80);

	void OnGUI(){
		for (int i = 0; i < 10000; i++) {
			GUI.Button(crash, "asdf","Box");
		}
	}

I have a bit laggy because it’s 10k times but the graphic isn’t doing that any longer

and I’ve figured out I kinda can’t update Rect every frame to different values without making new rect

and every new rect helps doing that

#EDIT:

figured out that time.deltatime does the trick tho don’t know why

if I change my camera from:

using UnityEngine;
using System.Collections;

public class CameraControll : MonoBehaviour {
    public float YSensitivity = 100 ;
    private float MouseRotationY ;

    void Update() {
        MouseRotationY += (Input.GetAxis("Mouse Y"));
        transform.localEulerAngles = new Vector3 (MouseRotationY * YSensitivity * Time.deltaTime , 0 , 0);
    }
}

to:

using UnityEngine;
using System.Collections;

public class CameraControll : MonoBehaviour {
    public float YSensitivity = 100 ;
    private float MouseRotationY ;

    void Update() {
        MouseRotationY += (Input.GetAxis("Mouse Y"));
        transform.localEulerAngles = new Vector3 (MouseRotationY * YSensitivity , 0 , 0);
    }
}

than camera isn’t jumpy any longer every 2. frame

tho this isn’t working for transform.position as that never jumps don’t know why the difference but that happenes for rotation

and why does that happen?

Time.deltaTime is something you use when you are trying to translate a value to be frame rate independent.

Time.deltaTime is the time it took to read the code in your scene and render the previous (last) frame.

If you have a fixed value - like “thrustForce” and you try to apply this every frame:

public float thrustForce = 10.0f;

void Update () {
   transform.Translate (Vector3.up * thrustForce);
}

… you should make this “frame rate independent”, by multiplying by Time.deltaTime. This way you tell Unity that you want to move the transform 10 meters per second rather than 10 meters per frame. Moving 10 meters per frame could vary depending on machine speed and the content of the scene as each frame could take a different amount of time to render.

Adding Time.deltaTime makes this code frame rate independent:

public float thrustForce = 10.0f;

void Update () {
   transform.Translate (Vector3.up * thrustForce * Time.deltaTime);
}

On the other hand, there are values that are not fixed in the same way as thrustForce = 10.0 because they occur within the frame itself, like Mouse Delta, which depends not only on how long the last frame took to render, but how fast the user moved the mouse in that time.

If you look at your code:

MouseRotationY += (Input.GetAxis("Mouse Y"));

… this is already “frame rate independent” as this is the distance travelled by the mouse for this frame.

If you read here: Unity - Manual: Input you will find that “Mouse X and Mouse Y are mapped to the delta of mouse movement.”, or the amount of movement the mouse has done that frame! There is, therefore, no reason to multiply this by Time.deltaTime.