Resetting a position

Ive made a game where there are two states of play: Play (where objects are affected by gravity and are not kinematic) and Stop (where they are not, and a player can move them around).

By all means I can think of, this following code should work, but it doesn;t.

    void LateUpdate () {

    if (playScript.gamePlays == false) { //if game is not playing
        if (playedLastFrame == true) { // but if game did play last frame
            transform.position = new Vector3(initialTransform.position.x, initialTransform.position.y, initialTransform.position.z);
            transform.rotation = initialTransform.rotation;
            transform.localScale = initialTransform.localScale; 
        }
        playedLastFrame = false;
        rigidbody.isKinematic = true;
        rigidbody.useGravity = false;
    } else {   //if game currently plays
        if (playedLastFrame == false) {   //if game last frame didn't play (so button was pressed)
            initialTransform.position = this.gameObject.transform.position;
            initialTransform.rotation = this.gameObject.transform.rotation;
        }   
        playedLastFrame = true;
        rigidbody.isKinematic = false;
        rigidbody.useGravity = true;

    }

}

}

In the playScript, there is a button that when pressed on (in the ONGUI function) sets gamePlays to true or false. At that point it should check wether or not the game was playing in the last frame. If it was playing in the last frame and isn't now,it should set the transform of the item to its initialTransform. But it doesn't. Why?

You don't really say what it is doing incorrectly, but if I had to guess, I'd suspect your problem is your `initialTransform`. Since you can't access the constructor for `Transform`, I imagine you are assigning it some other `Transform`. `Transform` is a reference type, so if `initialTransform == transform`, then `transform.position = initialTransform.position` is meaningless. You'll need to keep your position, rotation and scale as individual variables.

There's a bunch not included in the question that could be wrong with your variable declarations and their initial values as well.

You don't need `this.gameObject.` since `transform` is a publicly accessible member of `MonoBehaviour` which your class should inherit if you want `LateUpdate` to even be called.

If you were to simplify your variables and functions into something more legible, it would be much easier to follow what is happening and what is wrong.

Keep it simple

Why are you calling all this every frame and having to check your button state the last frame when you could just have your button call the function on the object(s) when it is pushed?

TogglePhysics.cs

using UnityEngine;

public class TogglePhysics : MonoBehaviour {
    public Transform target;

    Vector3 pausePosition;
    Vector3 pauseScale;
    Quaternion pauseRotation;
    string text;

    void Start() {
       if(target) {
           pausePosition = target.position;
           pauseScale = target.localScale;
           pauseRotation = target.rotation;
       }
       ButtonOn();
    }

    void OnGUI() {
        if(GUI.Button(new Rect(10, 10, 150, 100), text)) {
            if(text == "ON") ButtonOff();
            else ButtonOn();
        }
    }

    void ButtonOff() {
        text = "OFF";
        if(target) {
            if(target.rigidbody) {
                target.rigidbody.isKinematic = true;
                target.rigidbody.useGravity = false;
            }
            pausePosition = target.position;
            pauseScale = target.localScale;
            pausesRotation = target.rotation;
        }
    }

    void ButtonOn() {
        text = "ON";
        if(target) {
            if(target.rigidbody) {
                target.rigidbody.isKinematic = false;
                target.rigidbody.useGravity = true;
            }
            target.position = pausePosition;
            target.localScale = pauseScale;
            target.rotation = pauseRotation;
        }
    }
}

From the object perspective

To do it the way you have it would work like:

ToggleButton.cs

using UnityEngine;   

public class ToggleButton: MonoBehaviour {
    string text;
    public static bool ButtonOn;

    void Start() {
       ButtonOn();
    }

    void OnGUI() {
        if(GUI.Button(new Rect(10, 10, 150, 100), text)) {
            if(ButtonOn) ButtonOff();
            else ButtonOn();
        }
    }

    void ButtonOff() {
        text = "OFF";
        ButtonOn = false;
    }

    void ButtonOn() {
        text = "ON";
        ButtonOn = true;
    }
}

TogglePhysics.cs

using UnityEngine;

[RequireComponent (typeof (Rigidbody))]
public class TogglePhysics : MonoBehaviour {
    Vector3 pausePosition;
    Vector3 pauseScale;
    Quaternion pauseRotation;
    bool currentState;

    void Start() { //Initialize
       pausePosition = transform.position;
       pauseScale = transform.localScale;
       pauseRotation = transform.rotation;
       TurnOn();
    }

    void LateUpdate() {
        if(currentState == ToggleButton.ButtonOn) return; //we're good

        if(ToggleButton.ButtonOn) TurnOn();
        else TurnOff();
    }

    void TurnOn() {
        rigidbody.isKinematic = false;
        rigidbody.useGravity = true;
        transform.position = pausePosition;
        transform.localScale = pauseScale;
        transform.rotation = pauseRotation;
        currentState = true;
    }

    void TurnOff() {
        rigidbody.isKinematic = true;
        rigidbody.useGravity = false;
        pausePosition = transform.position;
        pauseScale = transform.localScale;
        pausesRotation = transform.rotation;
        currentState = false;
    }
}

If you knew nothing else was changing your booleans, you could even simplify boolean assignments to be `rigidbody.isKinematic = !rigidbody.isKinematic;` and the like.

Skovacs code is flawless except that if I got the question right, Jordi wants to return all the transforms to initial position when paused, so the snippet `

void TurnOff() {

   rigidbody.isKinematic = true;
   rigidbody.useGravity = false;
   pausePosition = transform.position;
   pauseScale = transform.localScale;
   pausesRotation = transform.rotation;
   currentState = false;
}

<p>should go as</p>

void TurnOff() {

   rigidbody.isKinematic = true;
   rigidbody.useGravity = false;
   currentState = false;
}
`

having the pause vars always setted to the starting position.

I see what you mean. I'll wrestle through the code, try it out this afternoon and update you on the progress. Thanks in advance ^^

It works for just one snag: it didn't recognise the difference between bool ButtonOff and function ButtonOff(), so just had to rename those. Awesome! Now off to making mouseOver and mouseExit GUI elements ^^