Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by Paparakas · Nov 26, 2013 at 11:48 AM · prefabvariableresetdefaultrevert

How do I reset a script's values at run time to what they were at start?

I've looked all over but I haven't found an answer. Say I have an array called MyPrefabs:

 public GameObject[] MyPrefabs;

I add the prefabs I want to the array in the inspector. Let's say I have a Cube object with a CubeScript attached. CubeScript has a speed variable which's default is 5.

 public class CubeScript
 {
  public int Speed = 5;
 }

I save the Cube with the CubeScript attached to a prefab and assign the prefab to the MyPrefabs Array.

I then decide that I want to modify that value.

 MyPrefabs[0].GetComponent<CubeScript>().Speed = 10;

So now let's say I want the default value again. How would I go about doing that? Obviously I could store the Speed variable in a separate variable, but that wouldn't really be feasible when I have multiple Prefabs each having multiple variables. I thought about copying the MyPrefabs array at Start to an Array called, say, MyPrefabsDefault then assigning MyPrefabs to the MyPrefabsDefault when I wanted to reset it to it's original values, but I'm not sure how that would work or if it's the best 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

3 Replies

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

Answer by clunk47 · Nov 26, 2013 at 06:36 PM

 // Inspector variables
 public float speed = 5;
 public float defSpeed;

 public void Start()
 {
     defSpeed = speed;
 }

 public void ChangeSpeed()
 {
     // change the speed variable somehow...
 }

 public void ResetSpeed()
 {
     speed = defSpeed;
 }
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
3

Answer by Jamora · Dec 04, 2013 at 08:24 AM

Instead of having duplicate variables; one for the actual value and one for the default, just use the Reset function. Set the default values manually inside. Reset is called when the reset-button (appears when right clicking on the Component name) is pressed or when the component is added for the first time. It is just a normal function, so nothing stops you from calling it yourself when you need to reset your values.

 public int speed;
 
 void Reset(){
     speed = 5;
 }


Comment
Add comment · Show 1 · 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 AdmiralThrawn · Dec 17, 2017 at 06:20 PM 0
Share

You forgot one aspect: The speed may not be initialized with 5 in the inspector, but maybe with say 7. Now, he wants to reset this value he needs some sort of backup variable that stored this "inspector set value".

avatar image
0

Answer by haim96 · Nov 26, 2013 at 12:00 PM

in your cubescript you could add another int for old speed so each prefab will save the old value for it self. i don't think there is away to avoid saving values for later use other then store them in variables.

Comment
Add comment · Show 3 · 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 Paparakas · Nov 26, 2013 at 12:20 PM 0
Share

Thanks for the answer. I don't think that making a duplicate variable for each single variable in every single class would be reasonable though. I hope there's another way.

avatar image PushpaK · Nov 26, 2013 at 12:54 PM 1
Share

You can just assign default values in start() and call start() again when you want to reset to those values.

avatar image haim96 · Nov 26, 2013 at 01:37 PM 0
Share

in fact, @Pushpa$$anonymous$$ is right! didn't thought about this. :) @Pushpa$$anonymous$$ you should write your comment as an answer because it seems to be the best way to achieve this.

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

21 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

Related Questions

Unity setting SerializedProperty.prefabOverride incorrectly 2 Answers

Reverting an object with children to its original prefab-state on runtime. 0 Answers

Storing scene-specific variables? 1 Answer

How to access a variable from a game object once instantiated (C#) 1 Answer

How to set a default prefab on a C# script? 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