Resetting Scriptable Objects - Unity Playmode

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
scriptable objects are a fantastic tool given to us by unity for our project they're these little data packets these little data prefabs that we get to use throughout our project and if you're not familiar with scriptable objects you might want to pause the video go search on Google go go search on YouTube there's a ton of resources and a ton of different ways to use them I'm a big fan personally and I use them throughout my project instead what I want to talk about in this video is one of the catches one of the tricks one of the problems that people often run into when they're using scriptable objects and that is resetting dat that's been changed in play mode in the editor I don't know if you've noticed around here but I don't very often do sponsored videos and there's a lot of good reasons for that that said almost every video on this channel while it doesn't have a sponsor it does have an inspiration and those Inspirations are my own personal projects I see some part of that project that I think is worth sharing and I make a video and this video is no different so if you found videos on this channel useful and you want to support the channel check out the links to the Steam and the itch page for deep space directive in the description down below if you change the data on a scriptable object at runtime or in play mode in the unity editor and then leave play mode those changes will persist out of play mode you can go back into play mode and those changes are still there now if you haven't run into this that might sound like strange Behavior especially if you're relatively new to Unity but the same behavior works for any asset such as a material if you go into play mode change the color on a material leave play mode that change to that color will persist outside of play mode doesn't matter how many times you go in and out that change will persist now there's a lot of people out there that would argue that you shouldn't change values on a scriptable object at runtime and that's fair enough I would argue on the other hand that there are use cases there are some use cases where it's a good idea or it's really useful and helpful to be able to change values on unscripted object at runtime but the problem comes in that you then when you leave play mode you leave runtime you need to reset the that data you need to reset those values so that the next time you go into play mode you're starting with those fresh values and that's that's a problem that I have run into I'm running into that with my stats and my upgrade system and now I knew that was going to be a problem when I design those systems but that was a problem for future me that was a problem that I would solve down the road I knew it was a solvable problem I just didn't know how to solve it so I figured I'd solve it when that time came and that time has come so as you do when you have a problem you get on Google and you do some searching and you see how other people solve that problem and what I found was two ways one I had no idea that it would work another idea that I just hadn't thought about but those two ways work really well to reset that data so let's look at those next so the first method to reset your scriptable object data is not the method I chose but it was a method that I didn't know existed and so I think it's worth sharing that is that somewhere along the lines somewhere in unity 2017 Unity added some of the life cycle functions from monob Behavior to scriptable objects and I had no idea that they had done that they've done that years ago what that means is that scriptable objects will have callbacks to awake un enable undisable and on destroyed but the catches is that they're called at times that make sense but feel different when you're used to the monob behavior life cycle the awake gets called when the scriptable object gets created now we can create scriptable objects at runtime but most of us are doing it outside of runtime so we're not going to see that call uh while we're playing or while we're testing so that's not a reliable place to reset your data on enable gets called when the resource gets loaded and that's going to happen anytime that scriptable object goes into the scene or has referenced in the scene so that's going to be a reliable place to reset our data on enable just blank out your Fields do whatever you need there on on enabled on disable you can guess is going to get called whenever that scriptable object gets unloaded so again it's going to be a reliable place to reset our data that's going to happen every time you go in and out of play mode undestroyed again gets called when that scriptable object gets destroyed you're not going to see that happen unless you're creating those script of objects at runtime in which case this is just not an issue one further Oddity to this is that when you go into play mode unity unloads all the scriptable objects and then reloads them so what I saw is that on disabled gets called and then on enable gets called when you enter play mode a little odd but again either one gets called or rather both of them get called when you enter play mode so they're both potentially reliable ways to reset data in a scriptable object so method number two this one's really simple and really easy to use and IT addresses the uh root of this issue in the root of the confusion in a mono Behavior if you have a private field that data is not serialized or it's not saved so if you go into play mode the value is reset if you leave play mode value gets reset that is unless you decorate that field with the serializable field attribute which a lot of us do all the time it's a very useful thing to do unscript objects on the other hand private fields are serialized so you go into play mode change the value of a private field leave play mode that value of that private field is saved it gets serialized so all we have to do to avoid that behavior is decorate that field with a non serialized attribute that's it that's it all we got to do is add a single attribute to the fields that we don't want to save any field that you might be changing the value of at runtime and you want to reset simply add the non-serialized attribute and you're good to go so there you go I hope that was interesting and better yet useful for you and your project until next time happy game Des signing I don't know if that's just rambly bull
Info
Channel: One Wheel Studio
Views: 2,018
Rating: undefined out of 5
Keywords: Unity3d, Indie Game, Game Development
Id: x8rQcJ6yfE4
Channel Id: undefined
Length: 5min 56sec (356 seconds)
Published: Wed Jan 24 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.