So I am making an endless runner using an asset I got on the store, works well but the only problem is that it recycles the game pieces rather than re-instantiating them. In my game you drive around and his objects like street lamps, and after you hit one and the platform gets recycled, the lamppost is already on the ground the next time you come around to the same platform. I wrote a script to put on any intractable objects but it does not seem to work, I will link it below though. Any help is appreciated thanks for your time!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public class resetPrefab : MonoBehaviour {
public Transform originalPosition;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (this.gameObject.activeInHierarchy) {
Debug.Log ("reset");
Vector3 currentPosition = new Vector3 (transform.position.x, transform.position.y, transform.position.z);
Quaternion currentRotation = new Quaternion (transform.rotation.x, transform.rotation.y, transform.rotation.z, 0);
currentPosition = originalPosition.position;
currentRotation = originalPosition.rotation;
}
}
}
Firstly, linking a Transform to your script like that isn’t really what you want to do here because when you get the position from that Transform, it’ll give you the current position of whichever object owns that Transform. You COULD make a second gameobject that never moves and get its transform this way, but that’s doing more work than you need to.
Secondly, I’m not 100% certain what you’re trying to do at the end there, but setting the local variables “currentPosition” and “currentRotation” like that won’t affect your gameobject’s position and rotation at all, that’s now how C# works. You have to set your gameobject’s transform’s position and rotation.
This is how I would do what you’re trying to do:
private Vector3 originalPosition;
private Quaternion originalRotation;
void Awake()
{
this.originalPosition = this.transform.position;
this.originalRotation = this.transform.rotation;
}
// call this function to reset this script's object's position and rotation
public void resetTransform()
{
this.transform.position = this.originalPosition;
this.transform.rotation = this.originalRotation;
}