• 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 /
  • Help Room /
avatar image
0
Question by impurekind · Sep 17, 2018 at 04:18 PM · animationssetactivereset-state

How to reset object and all its components, scripts, etc, to start state?

I have a few weapons, and when I fire one then quickly press the change weapon button mid-firing to select a new weapon (which sets the current weapon object to SetActive = false and the new weapon to SetActive = true), the current weapon seems to store all the animation positions, the ejected shell state, any timers in the scripts attached to it, and whatever else, such that if I cycle round to it again (setting it to SetActive = true again), it appears stuck half way through the previous reload animation and the shells suddenly finish ejecting and so on. And even if I press the fire button again, thinking this might at least go through the sequence and get the weapon back to its default state, it does the fire animation and stuf over the mid-reload animation so everything it messed up (the shotgun fires, with the kickback, but looking like it's bent in half and open so as to eject the shells).

What I want, whenever I cycle to a new weapon and then back again, is for each weapon to reset everything to do with it to the state it was in when it was first created in the game (but not by destroying and then instantiating objects as this is going to cause too many issue with the way my game is setup).

How do I do this properly? Is there a simple way I can do something like "Activate this weapon again but reset it as though it was being created/activated for the first time, so the animations are back to starting state and any scripts are running from their starting states and so on"?

Or am I just missing something here and there's possibly something I'm doing wrong that's creating an issue that could be resolved relatively easily some other way?

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

1 Reply

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

Answer by tormentoarmagedoom · Sep 17, 2018 at 04:28 PM

Good day.

I know there is this:

https://docs.unity3d.com/ScriptReference/MonoBehaviour.Reset.html

But an easy way to do it is to remove all its components and attach them again, for example a script called "BestScript"

  Destroy (Object.GetComponent<BestScript>();

and add it https://docs.unity3d.com/ScriptReference/GameObject.AddComponent.html

The only things that are not involved are relative to transform compoennt, which you wil need to set again (if needed) position, rottaion, scale and chiltparent relations.



Well, you have the

https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnEnable.html

which allows you to do what you want when the object becomes active. If you SetActive(false) it, and then (true) it will run the method again.

So if you configure your Start() and/or Awake() methods in OnEnable(), it will set all initial values all time becomes active.

Or directly, create a Reset() method, where you just Instantiate one copy of it, and destroy itself.

Bye!

Comment
Add comment · Show 7 · 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 impurekind · Sep 17, 2018 at 07:12 PM 0
Share

You'd think, given that it's possible for Unity to reset an object completely when you restart/reload a scene, that there would be some bit of code to do similarly for individual objects in your game. Basically just running like the level had just loaded for the first time.

Do you know if there's a way to tell Unity not to basically save the state of an object when you set it to SetActive = false, such that when I set it to SetActive = true again, it will have reset to its starting state with nothing saved (no position changes or animation changes or whatever)?

avatar image tormentoarmagedoom impurekind · Sep 17, 2018 at 10:13 PM 0
Share

Extended Answer.

avatar image impurekind tormentoarmagedoom · Sep 18, 2018 at 12:24 PM 0
Share

How would I configure the Start/Awake methods in OnEnable? You can't put the Start/Awake methods directly inside OnEnable, right?

Show more comments

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

91 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I reset both an object's parent and child positions and rotations fully? 0 Answers

Why isn't WaitForSeconds working? 1 Answer

Why isn't it switching the fireOn state to true 0 Answers

how to change between animations with pressing buttons 0 Answers

Im using the HQ Fighting Animation FREE pack and have a problem with the end of the animations 0 Answers


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