Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by UnityNoob123 · Mar 13, 2017 at 04:02 AM · transformprefabreset

How to reset a gameobject's transform?

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;
         }
     }
 }
 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by James2Games · Mar 13, 2017 at 04:59 AM

It looks like there isn't anything reassigning the objects position but instead saving it's position.

The line below is making a new position of the objects current position or rotation but rather saving the positions then doing nothing with them.

 Vector3 currentPosition = new Vector3 (transform.position.x, transform.position.y, transform.position.z);

The line below is changing what "currentPosition" contains to what the "originalPosition"'s position is.

 currentPosition = originalPosition.position;

Below is a solution and will change the position of the current object to have the same position as the prefab.

 transform.position = originalPosition.position;

Likewise for the rotation transform.rotation = originalPosition.rotation;

Final:

  if (this.gameObject.activeInHierarchy) {
      Debug.Log ("reset");
      transform.position = originalPosition.position;
      transform.rotation = originalPosition.rotation;
  }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Commoble · Mar 13, 2017 at 05:10 AM

Two things you're doing wrong here:

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;
 }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

83 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

ReplacePrefab() resets transforms for instanced prefabs? 1 Answer

transforming a prefab randomly at runtime 3 Answers

Reset Object 4 Answers

Resetting rigidbodies' transform values 1 Answer

Saving customized transform in game 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges