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.