• 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 Dr_GeoFry · Apr 20, 2012 at 01:35 AM · gameobjectreset-state

Resetting GameObjects After Dying

Hi Everyone,

I am curious if there is a simple way to reset the positions and states of gameObjects after the hero dies so that the whole experience starts over at the beginning of a new life of the hero. I am hoping it is simpler than coding for every object in the game because that seems extremely tedious. I am looking for some code examples to show me the way...

Thank you

Comment
Add comment · Show 4
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 crocodile5 · Apr 20, 2012 at 01:56 AM 0
Share

I am not sure I understand correctly but isnt restarting the scene just does that?

avatar image Dr_GeoFry · Apr 20, 2012 at 02:11 AM 0
Share

what do you mean by restarting the scene? How do you do that?

avatar image crocodile5 · Apr 20, 2012 at 02:19 AM 0
Share

I mean when the hero dies, game ends so the player needs to re-play the scene again, and the objects will be at their initial position.

avatar image Dr_GeoFry · Apr 20, 2012 at 02:27 AM 0
Share

How would you go about doing that? I want what you described to happen each time the hero loses a life. When the game ends, I would want it to return the main menu.

1 Reply

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

Answer by syclamoth · Apr 20, 2012 at 02:27 AM

Well,

 Application.LoadLevel(Application.loadedLevel);

will perfectly reset the scene, but is this really what you want?

Comment
Add comment · Show 4 · 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 Dr_GeoFry · Apr 20, 2012 at 02:37 AM 0
Share

this was exactly what I needed. I can't believe this didn't occur to me. I was only thinking of this to load new levels. Thanks!

avatar image aldonaletto · Apr 20, 2012 at 03:24 AM 1
Share

That's what this is doing: the current level is reloaded, thus everything goes to the original states. But you will need a life counter that doesn't get reset: declare it as static - static vars are created and initialized when the game begins, and survive to level changes:

 static var lifeCount = 5; // set to 5 only at game start
avatar image Dr_GeoFry · Apr 20, 2012 at 03:33 AM 0
Share

I definitely still need to set up lifeCounters and things like that. I am about to implement these things. I have code ready, but I just haven't set up the visuals for all that stuff yet.

Thanks for the added code.

avatar image syclamoth · Apr 22, 2012 at 11:48 PM 0
Share

Don't use static variables for this. That can only end badly. Use 'DontDestroyOnLoad(lifeCounter)' to stop specific objects from being removed. Avoid static variables whenever you possibly can.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

Reset a Gameobject to the original state 0 Answers

How to reset some specific game objects in the scene via a button? 2 Answers

Football penalty game- Move gameobject in direction of swipe with force 0 Answers

Restricting "Drag GameObject" script to Z Axis? 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