I have a simple script that should grab a GameObject's transform position and rotation then move the GameObject back to that position when it's hit by a Raycast. (Objects get moved around as the player bumps into them).
Its working fine for the objects that already have the script attached but when I apply the script to a new GameObject, it resets the transform's values to (0,0,0),(0,0,0) in the scene editor.
I'm sure there's a smarter way to do this.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TransformReset : MonoBehaviour
{
private Transform LocalTransform;
private Vector3 InitialPos;
private Quaternion InitialRot;
private Vector3 CurrentPos;
private Quaternion CurrentRot;
void Start()
{
InitialPos = transform.position;
InitialRot = transform.rotation;
}
public void Reset()
{
transform.position = InitialPos;
transform.rotation = InitialRot;
Debug.Log("resetting position of " + gameObject.name);
}
}
that "public void Reset()" part is called by my raycasting script. But I think the problem is that its running in the scene editor before ever getting a useful "InitialPos" value.
Appreciate any help and advice.