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
2
Question by Wrymnn · Jan 31, 2015 at 07:53 PM · recompile

Updating code at runtime, without loosing all initialized variables

Hi there I know it is possible to update code at runtime in Unity3d. I run the game, change the code, save the script and the game updates itself.

- However my question is: Is there is option or way how to do this without loosing all my saved variables? Thus breaking entire game after saving your code in runtime.

Let`s say you initialize your variable when game starts, and load a texture to that variable. Now you run the game, go to change your code (game is still running), make changes, save the code, but when game (recompiles) at runtime, it resets all the variables. Thus your saved texture will reset itself, causing critical errors and null references.

Comment
Add comment · Show 5
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 AngryBurritoCoder · Jan 31, 2015 at 11:53 PM 0
Share

why change scripts during runtime ?

avatar image DESIMA · Feb 01, 2015 at 12:12 AM 0
Share

at runtime you can change your parameter in inspector you dont need to change in code, and code wont always be correctly updated either. when you exit play mode in unity your changes in the inspector will be gone, so write them down.

if you wanted so backup your data before overwriting them you can output to xml or unitys preference method.

avatar image AngryBurritoCoder · Feb 01, 2015 at 12:24 AM 0
Share

ah xd, it is made on purpose to delete any changes done at runtime, it is basically a play test for you to check the best settings, so it is best to never change too much in play testing, you should use it for small modifications then exit it change it and then play test again

avatar image Wrymnn · Feb 01, 2015 at 07:28 AM 2
Share

why change scripts during runtime?

I cannot even believe you are asking that xD

at runtime you can change your parameter in inspector you dont need to change in code, and code wont always be correctly updated either. when you exit play mode in unity your changes in the inspector will be gone, so write them down.

if you wanted so backup your data before overwriting them you can output to xml or unitys preference method.

DESI$$anonymous$$A I want to change code not simple parameters in Inspector....Code in scripts like add stuff to function etc. (Like eclipse/java uses with hot-swapping)

avatar image DESIMA · Feb 01, 2015 at 03:35 PM 1
Share

You cant do that in unity. Unity will compile everytime you make changes, it is not hot swappable

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by babaji1234 · Feb 01, 2015 at 07:13 AM

Pretty simple, just check if your texture is null, and if it is, then reassign it

 Texture2D texture;
 
 void Update () {
  
  if(texture == null) {
    texture = LoadTexture();
  }
 
 }
 Texture2D LoadTexture () {
  //set your texture here
 }
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 Wrymnn · Feb 01, 2015 at 07:35 AM 0
Share

Yeah this was pretty good idea, but The only problem I see is with GUI. All GUI breaks, but the textures stay loaded. And I get Null reference exceptions at stupid lines where it should give any errors. Like here:

 Xpos = 5;
 Ypos = 5
 
  GUI.Label(new Rect(Xpos, Ypos, 20, 50), "Something", myStyle);
 //myStyle defined somewhere else

And this entire gui.label will get null or something, like its part of GUI class problem that the GUI class will get crazy.

avatar image
0

Answer by team_eden · Feb 01, 2015 at 02:08 AM

You can drag and drop items into exposed variables within the inspector, and they will stay persistent on play as long as they are part of the same prefab.

You can try copying the component during play, and pasting it after you stop the scene again.

If the object is something complex, I suggest exporting the data to a file, and loading it again on playback.

Otherwise 'load a texture' is quite vague, I assume you mean drag and drop. In which case, take my first suggestion.

Good luck

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 Wrymnn · Feb 01, 2015 at 07:05 AM 0
Share

Haha no xD my game is quite complex to do everything by inspector it isn't option anymore.Yeah I also use load texture. But there are other problems like when you play the game and chage variables during gameplay like select your hero and there is variable that holds selected gameobject. It will reset...

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

22 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

Related Questions

Playmode script recompilation - static vars lost? 0 Answers

CommandInvokationFailure: Failed to re-package resources 0 Answers

Serialize array of custom class in custom class 1 Answer

Unity Re-compiles on Play 1 Answer

Keep network socket open during play mode recompile 2 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