Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
2
Question by Moyjaan · Jun 03, 2017 at 11:31 AM · c#respawnreset

I want to reset my object to its original position.

Hey guys,

I'm creating a game where I want a car to pass by over and over again. I know how to let the object move but I don't know how to reset it so it starts again at its starting point. I'm trying it like this but nothing happens, tried to find the right answer but there's none. I'm a noob in C#, do you also have tips to help me getting better at C#

public class Rewspawn : MonoBehaviour {

 void Start()
 {
     
 }

 void OnTriggerEnter(Collider other)
 {
     Vector3 orginalPosition = gameObject.transform.position;
     if(other.gameObject.tag == "End")
     {
         gameObject.transform.position = orginalPosition;
     }
   
 }

}

Comment
Add comment · Show 1
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 SystemSuitable · Feb 01, 2020 at 04:20 AM 0
Share

//Note exObj is the object you want to use to set the original position, this works with images as well with images you can turn down the alpha to 0 and it will be invisible, also this script must go on the object that you want to move back to the original position or you can make a public GameObject myObj set it as the object you want to set back and change transform.localPosition = originalPosition; to myObj.transform.localPosition = originalPosition;

public Vector3 originalPosition;

public GameObject exObj;

void Start () { originalPosition = exObj.transform.localPosition; }

void OnTriggerEnter(Collider other) { if(other.gameObject.tag == "End") { transform.localPosition = originalPosition; }

}

2 Replies

· Add your reply
  • Sort: 
avatar image
12
Best Answer

Answer by FM-Productions · Jun 03, 2017 at 12:57 PM

Store the original position in the Start or Awake function:

 Vector3 originalPos;
 
 void Start()
  {
      originalPos = new Vector3(gameObject.transform.position.x, gameObject.transform.position.y, gameObject.transform.position.z);
 //alternatively, just: originalPos = gameObject.transform.position;
 
  }
  void OnTriggerEnter(Collider other)
  {
      if(other.gameObject.tag == "End")
      {
          gameObject.transform.position = originalPos;
      }
    
  }



Comment
Add comment · Show 2 · 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 Moyjaan · Jun 06, 2017 at 10:11 AM 1
Share

Thank you, this helped me a lot. Also helps me better understanding C#.

avatar image ransomink Moyjaan · Jun 06, 2017 at 02:05 PM 1
Share

alternatively, you can do: originalPos = transform.position;

and when comparing tags, use CompareTag(), like so:

 void OnTriggerEnter ( Collider other )
 {
     if ( other.CompareTag ( "End" ) )
     {
         transform.position = originalPos;
     }
 }
avatar image
0

Answer by perrypower95 · Aug 31, 2021 at 04:49 AM

Brilliant!

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

11 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

C# Respawn Help 1 Answer

Respawn Script Problem 1 Answer

Restrict camera rotation 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