How do I move my GUI around?

Disclaimer: Please read the entire post before posting; I don’t want people to answer the question as if the ‘title’ of the post was the only thing I wrote.

I’ve been trying to make a main menu GUI box move from one area to another. I want the box to move from the right of the screen to the center of the screen. Here’s some code I found online:

`using UnityEngine;
using System.Collections;

public class MainMenu : MonoBehaviour {

public Texture icon, icon2;   

public float myBoxLeft = -200.0f;

void Update () {
    if (Input.GetKeyDown(KeyCode.Space)) {

void OnGUI() {        
    GUI.Box(new Rect(myBoxLeft, 20.0f, 220, 720), icon);
    GUI.Button(new Rect(myBoxLeft, 160.0f, 120, 30), "Click");
    GUI.Box(new Rect(myBoxLeft, 200.0f, 120, 120), icon2);

void AnimateGUI() {
    if (myBoxLeft == -200.0f)
        while (myBoxLeft < 20.0f)
            myBoxLeft += 5.0f;
    else if (myBoxLeft == 20.0f) {
        while (myBoxLeft > -200.0f) {
            myBoxLeft -= 5.0f;

I tested it, and it works in a rudimentary sense (the box will appear on the left then further to the right when you press the ‘Space’ key), but I’m still unsure as to how I can make the object move at, say, 5 units per second.

myBoxLeft += 5.0f*Time.deltaTime;

deltaTime is the time elapsed since last frame. Your way means 5/frame. Above you have 5/second.

Principle is if you have 50fps then yuo get deltaTime = 0.02. Then using this to multiply the value you make your 5 happen over a second.

It also allows to make your game computer dependant.

If you run the same code on a 100fps machine then the multiplier is 0.01 but it is used 100 times so in the end you still get 5 over a second.

Now one side note, avoid


you are really likely to get problem as float are nbarely 20 sharp but more likely 20.00000001 which is not 20.0. You should prefer:


I would suggest using animation, or iTween, that makes these movements easy.

I got help from another website to create a rudimentary animation to scroll from left to right when the game first starts. Basically, the value for the box’s position on the x-axis is increased over time at X amount of spaces until it hits a limit set up for its right-hand side. Here’s the code:

`//moves GUI at start of the program
void AnimateGUIAtStart() {
bool atRight = false;
float moveSpeed = 0.0f;
float alphaChangeSpeed = 0.0f;

    if (atRight == false)
        moveSpeed = 5.0f;
        alphaChangeSpeed = Time.deltaTime;      //fade into view by changing alpha channel to 1.0f

    if (moveSpeed != 0.0f)
        boxPosX += moveSpeed;
        alphaNum += alphaChangeSpeed;

        //sets values of boxPosX, alphaNum to limits if they go above limits
        if (atRight == false && boxPosX >= boxLimitRightX)
            boxPosX = boxLimitRightX;
            moveSpeed = 0.0f;
            alphaNum = 1.0f;
            alphaChangeSpeed = 0.0f;
            atRight = true;
            //isMovingLeft = true;

The box can be moved back to the left if you set up the boundaries, moveSpeed, etc. to their respective opposites.

Unfortunately, I’ve found if extremely difficult to use this for when you want to switch over to another menu (e.g. if you want the animation to occur after pressing a button that would take you to another menu).