No video

Unity3D - Saving and Loading your Inventory with Scriptable Objects | Part 2

  Рет қаралды 93,460

Coding With Unity

Coding With Unity

Күн бұрын

In this video we go over how to save and load an inventory, made of a scriptable object, that is populated with items (which are also scriptable objects)
How to make an Inventory using scriptable objects: bit.ly/2nHUac5
Source Code: github.com/sni...
If you like this channel, or just Unity in general, consider joining my Discord at: / discord
********************************************************************
● Support me with Brave: brave.com/cod828
♥ Subscribe: bit.ly/2FRWgOi
● Facebook: bit.ly/2DhBC6I
● Twitter: / indie_unleashed
********************************************************************
● Make a Grid with a 1 dimensional array: bit.ly/2sRTpfL
● How to make Snake in Unity Tiny: bit.ly/2UmJHhe
● Climbing System Show off: bit.ly/2FPurq1
********************************************************************

Пікірлер: 379
@techno_tuna
@techno_tuna Жыл бұрын
I just want you to know your tutorials are not only helping me make an inventory system, but helping me to learn a LOT of stuff around it as well.
@zero-to-100
@zero-to-100 3 ай бұрын
As a non-English speaker, Other lectures are hard to listen. However, this lecture is well explained and easy to understand to your voice full of passion and confidence. The quality of the lecture is very good. I'm glad I searched for the ScriptableObject Inventory lecture before implementing the inventory function as "Playerprefs". (It's terrible just to imagine.) Best lecture ever, Best teacher ever. I shared it to another developer. Have a great day.
@jenniferroth7824
@jenniferroth7824 4 жыл бұрын
This is a great series! Easy to follow and gives solid information. Also like how you are layering the concepts but not getting too far into the extra details.
@CodingWithUnity
@CodingWithUnity 4 жыл бұрын
Thank you! :)
@brandons2501
@brandons2501 4 жыл бұрын
Here after watching the first video, I love the way you explain certain things, it makes it really clear (although I'm still pretty new so I don't really understand most things yet... hopefully, that'll change) anyway just want to say thank you. This is an amazing series and I intend to watch the whole playlist.
@CodingWithUnity
@CodingWithUnity 4 жыл бұрын
Great to hear! Hope you learn something :)
@neilamess237
@neilamess237 4 жыл бұрын
I don't normally comment on videos but I feel in this case I have to. You are by far one of the best tutors in C# in Unity that exists on KZfaq. You explain clearly what you are doing and why and in such a way I can actually understand you; and it makes a lot of sense. However, I am experiencing the same problem as elcamp3 in that my Save and Load procedures only work if I use them as soon as the game starts (with no items in the Player Database). Once I collect some objects they no longer fire. Will continue to try and solve the issue (I hate to give up) but any information you can provide will be much appreciated. Keep up the excellent tutorials, I look forward to finishing this project.
@CodingWithUnity
@CodingWithUnity 4 жыл бұрын
Are you getting errors? Be easier to help you on discord, so feel free to join it!
@neilamess237
@neilamess237 4 жыл бұрын
Thanks, but I solved the problem. There was nothing wrong with my code, I can't believe I'm going to admit this. When I used my player to collect some objects the focus was on the Scene View so none of the keys would work in game view. Once I switched focus to Game View everything worked. That is a mistake I will not be repeating.
@CodingWithUnity
@CodingWithUnity 4 жыл бұрын
@@neilamess237 Hahaha that's hilarious :)
@latexgrime
@latexgrime Жыл бұрын
You are amazing at explaining everything, you don't have a clue on how much you're helping me, thank you so much
@hmcq6
@hmcq6 4 жыл бұрын
These tutorials are awesome! Thank you!! I've been programming 20 years but I've always found Unity intimidating. These tutorials are perfect, better than the selection on Unity Learn Premium.
@CodingWithUnity
@CodingWithUnity 4 жыл бұрын
Really glad your enjoying them! Also awesome to hear i appreciate it :)
@mehmedcavas3069
@mehmedcavas3069 3 жыл бұрын
these are really valuable tutorials that are a must for developers. If you want to make a real game then these kind of videos are important
@huyopo
@huyopo 4 жыл бұрын
Just for notice: it's a myth that the + operator for strings is less performant. Somehow everyone seems to believe that, but it's not true. It's actually in most cases the most performant. The myth comes from when you do iterations where you add stuff to a string, it can take a lot of time, as every iteration it creates a new string, but if you just do it inline, the compiler knows it and does it in a optimal way.
@MyCiaoatutti
@MyCiaoatutti 3 жыл бұрын
Exactly. Even Microsoft recommends using the + operator. Basically, by using the + operator, the C# compiler will evaluate which concatenation method to use based on performance. Sometimes, you just have to trust the compiler and write simple code
@stevenjeanne4156
@stevenjeanne4156 2 жыл бұрын
@@MyCiaoatutti I Believe microsoft recommend using the String Interpolation operator $"{variable} {variable2}".
@WelshGuitarDude
@WelshGuitarDude 2 жыл бұрын
The performance hit is due to heap allocation and unity has a poor outdated garbage collector, so it's more unity specific
@nikoart1561
@nikoart1561 7 ай бұрын
Absolutely disagree using + generate more garbage collection. There's a better solution is using ZString in order to use concat, direct text without any allocation, Join and Format. Each + operator allocate more and more garbage until your game start to have some stutters and lag
@toaitran7444
@toaitran7444 4 ай бұрын
Man, there are so many inventory issues that have been bothering me for 2 weeks, today I'm so lucky to have found your video. I like the way you explain when writing code, as there are many techniques if you don't explain, then I wonder: how can I learn this on my own? I love you! Wishing you many interesting experiences in life!❤
@Dan-Levi
@Dan-Levi 4 жыл бұрын
Been following along and just wanted to say thank you for providing us with great Unity content. Greetings from Norway 👍👍👍😃
@CodingWithUnity
@CodingWithUnity 4 жыл бұрын
Hilsener! Thanks a lot, glad your enjoying it
@exon878
@exon878 3 жыл бұрын
@@CodingWithUnity u from norway or just google translate?
@Keolamation
@Keolamation 2 жыл бұрын
yeah, not many people realize how important it is to securely write out objects and write them in for MMORPGs thank god you made this. I was using referencde structs to populate my games items before i saw this and saw the callback receiever. now, im gonna experiment with this for another week or two and see if i can get result i like. xD
@mrman7009
@mrman7009 4 жыл бұрын
Thank you for putting out these videos, they are excellent, straight to the point and I like that you started from scratch in Unity with these tutorials. Because of this I can understand what each part of your code does a lot easier. Learnt a lot so far!
@CodingWithUnity
@CodingWithUnity 4 жыл бұрын
Im glad your enjoying them!
@laggard31
@laggard31 4 жыл бұрын
T H A N K Y O U SO MUCH! I've learned SO SO much from just these 2 videos so far!
@CodingWithUnity
@CodingWithUnity 4 жыл бұрын
That is awesome!
@galdar9618
@galdar9618 2 жыл бұрын
Learned nothing yet, but!.. REAL learning gonna begin when I'll try to make this system fit for my game
@duane7032
@duane7032 2 жыл бұрын
Great ! no problems at all working very smoothly so far about to start the next episode !
@sodanice3214
@sodanice3214 Жыл бұрын
What a great tutorial to learn not only the inventory but also the basics of the save function!
@sevemz3359
@sevemz3359 3 жыл бұрын
Thanks, it works on android. For those who want to use it on android, you just need to plug it to a save and load function
@JasonWelch
@JasonWelch Жыл бұрын
Damn, the item database solution is brilliant! I can think of a number of improvements to it such as creating different database assets for different item types, but I think this is the solution I'm going to go with. Thanks!
@JasonWelch
@JasonWelch Жыл бұрын
Finished implementing this last night. Wound up making a base ItemDatabase so that I could extend it with specific item types.
@user-hm2is2ex9f
@user-hm2is2ex9f 8 ай бұрын
Thanks a lot. This helped me a lot in understanding to write my own scriptableobject. Great stuff. It's a shame you no longer making new videos.
@davescellphone5376
@davescellphone5376 3 жыл бұрын
I think, thank you for these videos. I'll have to watch about 19 other tutorials to understand most of what you're talking about but it looks far beyond what I will need.
@Bulletforpaul
@Bulletforpaul 4 жыл бұрын
Thanks for your videos. I am learning a lot with these. A suggestion would be, instead of using the double Dictionary we can just use the index i of the items array. the code for this in the ItemDatabaseObject.cs would then look like this: public ItemObject GetItemById(int _id) { if (_id >= items.Length) { return null; } return items[_id]; }
@chrisflynn5192
@chrisflynn5192 4 жыл бұрын
Just posted a comment asking if there was any real reason not to do exactly this. Seems over complicated and unnecessary but thought I was maybe missing something...
@ryanbeattie9170
@ryanbeattie9170 3 жыл бұрын
This does indeed seem to work for me. Thanks for the comment!
@stevenjeanne4156
@stevenjeanne4156 2 жыл бұрын
@@chrisflynn5192 If you want to use GUID as your ID, you won't access your array by index. It works here because int _id is an "int". And the ID of the object are their indexes. If you have an Item[5] and one of your item has 123 as it ID. Accessing items[123] will throw an exception with ArgumentOutOfRange. While with a dictionnary you could have [123] : Item [235] : Item [1] : Item or if you key is a string ["wood"] : Item ["water"]: Item ["somethingelse"]: Item. Then you access your item dictionnary with : MyDictionary[Item.Id].
@meandmyself1600
@meandmyself1600 3 жыл бұрын
Thanks a lot for this tutorial! I like the idea of scriptable objects but was a bit stuck with the saving part. Ended up with some wicked text replacement within the json string. Those After/BeforeSerialization methods are a blessing.
@yapsharlene
@yapsharlene 4 жыл бұрын
super big thanks!! i love your content! you make these really easy to understand. 😊💕
@CodingWithUnity
@CodingWithUnity 4 жыл бұрын
Thank you
@shakedlevi32
@shakedlevi32 2 жыл бұрын
Remarkable!, Ending the day with a new part from this tutorial is very relaxing and teaching. Thanks man
@CodingWithUnity
@CodingWithUnity 2 жыл бұрын
Glad it's helpful!
@neffthepanda
@neffthepanda Жыл бұрын
Another great video! Loving this tutorial series.
@prototype21
@prototype21 4 жыл бұрын
I'm 2 weeks into Unity & coding so this is over my head but somewhere before you changed the database from public to private, the item ID paramater in player inventory no longer works in identifying which item you are loading out your inventory with. If you hit play while your player inventory's load-out is set to container size at 0 and or items set to none, picking up items while colliding won't work. In other words, you have to populate your player inventory with every item before hitting play otherwise you won't be able to pickup the said item.
@wellitriedig
@wellitriedig 3 жыл бұрын
Same for me
@TheJakeblake
@TheJakeblake Жыл бұрын
Thank you very much for this it was really helpful, now I'm going to continue onwards with the series.
@niklaskras5498
@niklaskras5498 3 жыл бұрын
uff gotta watch it again. i´m new to all of this but thx for the video!
@MrRipper55
@MrRipper55 2 жыл бұрын
Following along trying to understand some of the concepts, but I'm quite new in programming so this one was tough - at least it works :) I have no idea what most of this code means for now, ill try my best to understand it. Anyways the best inventory system I've seen so far, thanks for sharing your knowledge, im very grateful!
@bossmanstudios544
@bossmanstudios544 3 жыл бұрын
why am I getting a: "unityexpection; tostring is not allowed to be called during serialization, call it from onenable instead" when I add a Item to the database? please help because theres none online.
@zach31194
@zach31194 3 жыл бұрын
I am getting this too. It has something to do with the items array but I cannot figure it out. I even created a TestObject and a TestItemObject and removed all the fields from them and made the items array of that type, and still get the error. Google has no help, as this error seems to only have just surfaced within the last few version of Unity. The Unity docs don't have any mention of this error either, which is a shame, because it would be helpful. I will keep digging to see if I can figure it out, but until then, at least Unity version 2020 and up cannot follow this tutorial. :/
@bossmanstudios544
@bossmanstudios544 3 жыл бұрын
@@zach31194 I just gave up and followed brackeys tutorial and it worked, or you can restart this tutorial since that usually works for me.
@MattieSheldonTheYoutuber
@MattieSheldonTheYoutuber 3 жыл бұрын
I'm getting the same problem. Seems to be to do with the latest version of Unity.
@alexgroupman1282
@alexgroupman1282 3 жыл бұрын
Same issue here - shame to have to switch to a new tutorial over one issue. Anyone got ideas on how this can be fixed or worked around?
@alexgroupman1282
@alexgroupman1282 3 жыл бұрын
Never mind - after continuing with the tutorial this error disappeared, just got hasty with the comment!
@nv7287
@nv7287 2 жыл бұрын
This is such an amazing tutorial - I hope you dont mind, but If I could make a suggestion... to do a code review off the classes... it would help with debugging... (often downloadble projects dont nessasrily match what is happening in each stage, which makes it tricky to see where Ive gon wrong...) None the less this is really the best tutorial Ive seen so far on this topic - and Im so thankful for it!
@spokloo
@spokloo 2 жыл бұрын
hey when i press enter multiple times, it loads and displays the slots over the existing ones, how can i fix this ?
@masonmason22
@masonmason22 2 жыл бұрын
Very helpful. Thank you so much for making these videos.
@EduardoYamauchi
@EduardoYamauchi 4 жыл бұрын
This video is very useful and didactic, please make more videos like that!
@eleocraft278
@eleocraft278 2 жыл бұрын
Does not work because the OnAfterSerialize call order is random, sometimes i just get an error because the InventoryObject is trying to access an empty dictionary inside of the ItemDatabaseObject
@thieukhai396
@thieukhai396 3 ай бұрын
Same here. It's always null database because OnAfterSerialize will run before OnEnable.
@berkalkan3382
@berkalkan3382 3 жыл бұрын
best tutorial i've ever watched so far!
@kl3mta3
@kl3mta3 3 жыл бұрын
So in the editor everything works perfectly, and when i build it completes with no errors, and saving/loading works. However, my player when walking over the item prefabs, doesn't pick them up or add them to the inventory. If I build the game with items already manually added to the player inventory, then it works as intended. Again, only for clarification, if I hit play in the editor with the player inventory empty it works, but will only work after built if the inventory has items already in it. Would anyone know why this happens? I've checked my code with the tutorial a few times and cant see any difference.
@bladyops9900
@bladyops9900 Жыл бұрын
I know this is one year after you posted this, but in case anyone else has this issue, make sure your path is correct to your database after you move it to the resources file! In your “OnEnable” method in your InventoryObject script, the path file should be “Assets/Resources/Database.asset”. Hopefully this helps at least on person!
@stevenjeanne4156
@stevenjeanne4156 2 жыл бұрын
At 14:30 you could use using FileStream. (Thus not needing to call Close). And Use Path.Combine instead of string.concat().
@Dmitri-kz7gs
@Dmitri-kz7gs 6 ай бұрын
Hi! Can i make, that saving system is working every second(update()), and load the inventory in start?
@alexold4126
@alexold4126 3 жыл бұрын
when i build and run , in the game save&load dont work, and my player dont interact with objects :/ can someone help me with that? In unity all works good. but in game no :(
@infernumex
@infernumex 2 жыл бұрын
I'm having a bizarre issue with this system where InventoryObject.OnAfterDeserialize is always throwing a null reference exception. I did some head scratching and googling around, and it looks like it *might* be a Unity issue where references aren't kept after deserialization. Has anyone else had this issue? Unfortunately, I'm not experienced enough to code up a workaround.
@Golden_Hephaestus
@Golden_Hephaestus Жыл бұрын
Hey! I'm having the same problem, were you able to find a workaround?
@mrworld8494
@mrworld8494 3 жыл бұрын
ok so i have a problem the inventory is not showing the items when i build it but runs perfectly in editor mode help!
@Legoldos
@Legoldos 3 жыл бұрын
I got error KeyNotFoundException: The given key was not present in the dictionary. But code works as shown in video, so I tried debugging it and I find out that OnAfterDeserialize() on InventoryObject is called (paralel) before OnAfterDeserialize on ItemDatabaseObject. So when I am populating Inventory with items the dictionaries are empty hence the error. I added check if database contains key, so I got rid of error and code still works as expected, but I am wondering if there is better solution to this. Anybody knows?
@KeithK469
@KeithK469 3 жыл бұрын
Is it possible to post your code? I'm attempting your solution, but cannot get it to work.
@Legoldos
@Legoldos 3 жыл бұрын
@@KeithK469 if (database.GetItem.ContainsKey(Container.Items[i].id)) Container.Items[i].item = database.GetItem[Container.Items[i].id]; but you wont need ti, whole system gets reworked in next parts of this tutorial
@KeithK469
@KeithK469 3 жыл бұрын
@@Legoldos Thank you very much! I appreciate your response.
@marcus_vdm
@marcus_vdm Жыл бұрын
Ok, but how do you Delete the saved data?
@darzeon
@darzeon Жыл бұрын
I like the way you say IDE 😋
@CodingWithUnity
@CodingWithUnity Жыл бұрын
Lmao, gotta love my southern accent. It's hard to get rid of loll
@DakotaWinz
@DakotaWinz 2 жыл бұрын
this dude is bonk and all over the place... trying to follow the tutorial... good explanations but crazy execution O_o
@genuinefraud8262
@genuinefraud8262 4 жыл бұрын
KeyNotFoundException: The given key was not present in the dictionary. System.Collections.Generic.Dictionary`2[TKey,TValue].get_Item (TKey key) (at :0) InventoryObject.OnAfterDeserialize () (at Assets/ScriptableObjects/Inventory/Scripts/InventoryObject.cs:48)
@RequiemsVoid
@RequiemsVoid 3 жыл бұрын
You ever find a solution to this problem?
@genuinefraud8262
@genuinefraud8262 3 жыл бұрын
@@RequiemsVoid unfortunately no, gave up on the project though
@genuinefraud8262
@genuinefraud8262 3 жыл бұрын
@@HouseTrapJay same
@nakkah8614
@nakkah8614 3 жыл бұрын
Same error here :(
@nakkah8614
@nakkah8614 3 жыл бұрын
I have found the solution ! U need to reference your items (sword, monkfish, bones ...) on your Database !
@Mr.NorwegianMarcus
@Mr.NorwegianMarcus 4 жыл бұрын
I may be doing something wrong but when I pick up an item and save, then pick up 1 or more items of a different type and then finally reload it seems like the items that isn't saved dosen't get removed VISUALLY...It's like it UI dosen't update when going to remove slots it just adds them
@Wolfie5309
@Wolfie5309 4 жыл бұрын
at 11:42, you stated that you had five items in your inventory, when you clearly counted 6, and the list size also says six. Lists/Arrays/etc start at index 0, and count up from there. I know you know this, but beginners might not. Just a note, that's all.
@CodingWithUnity
@CodingWithUnity 4 жыл бұрын
loll woops
@crossfire9923211
@crossfire9923211 4 жыл бұрын
If you're here and you're a "beginner" then you are in the wrong place. If you don't understand how Lists/Arrays work you clearly need to go back to learning C# then find something that fits your plate, which is clearly not an inventory system.
@Wolfie5309
@Wolfie5309 4 жыл бұрын
@@crossfire9923211 be that as it may, it's still a valid point that I made. And beginners can be here if they're looking to challenge themselves.
@stevenjeanne4156
@stevenjeanne4156 2 жыл бұрын
@@Wolfie5309 The code in this video isn't a challenge for a beginner. It'll be hard for them if they don't know the basics of coding.
@RTWrename
@RTWrename 2 жыл бұрын
I can run this in editor no problem, but when I build the game I get NullReferenceException: Object reference not set to an instance of an object InventoryObject.OnAfterDeserialize () on the line " Container[i].item = database.GetItem[Container[i].ID];" If some one knows this problem would apreciate help
@hirosacraftsmp8278
@hirosacraftsmp8278 2 жыл бұрын
I have the same error
@terrileebrunner9244
@terrileebrunner9244 Жыл бұрын
Same here and I've checked the code multiple times for any errors. There's no other error... :(
@Golden_Hephaestus
@Golden_Hephaestus Жыл бұрын
Same here, has anyone found a workaround?
@ashfidislam1321
@ashfidislam1321 4 жыл бұрын
Always excited to watch your videos man! What are we seeing next btw?
@CodingWithUnity
@CodingWithUnity 4 жыл бұрын
Im going to do a few more videos on this series, i want to show how to make an equipment screen and a bank Im also considering making a combat system and combining the leveling/inventory/and combat system videos into a game in another video (but dont hold me too it) Im also following the new dots networking and may consider doing videos on that down the road.
@ashfid8987
@ashfid8987 4 жыл бұрын
Coding With Unity Oh man, this is exciting. Really loving the RuneScape related videos. Rarely I find videos with inventory system or any system that relates to RuneScape. Great game to get inspired on various systems. I am really liking Dots too btw. Are you working on any real project right now?
@hotadoga00789
@hotadoga00789 4 жыл бұрын
@@CodingWithUnity Would you be willing to try, or show, some sort of basic crafting system. Combining two or more items to create a third item?
@CodingWithUnity
@CodingWithUnity 4 жыл бұрын
That should be simple to do, ill think about it and see if i can make a video on it near the end of this series!
@therondoshow
@therondoshow 2 жыл бұрын
I'm a little confused about where the save files are created? It seems the save file when playing inside the editor is different than with a build. I'm on a Mac - and when I play in the editor it gets saved to Library/Application Support/DefaultCompany/ProjectName/. But the Build seems to be saving elsewhere because if I delete that file at that path, and create a new one with the build, it doesn't show up in that folder. The saving/loading functionality IS working though. Any idea where the build is saving the file? I'm so confused.
@buraqalaeli6619
@buraqalaeli6619 2 жыл бұрын
Amazing and informative tutorial! Recommend 10/10 would buy again... :P
@joshuahoem8740
@joshuahoem8740 Жыл бұрын
Hey getting an error all of a sudden for the item database script saying "ToString is not allowed to be called during serialization". It points to the line "GetId.Add(Items[i], i);"
@CodingWithUnity
@CodingWithUnity Жыл бұрын
Hard to say for sure, if you join my discord may be easier to help figure it out
@joshuahoem8740
@joshuahoem8740 Жыл бұрын
@@CodingWithUnity Joined, but not sure what channel to throw the question into. Its in SO Inventory for now
@marularch
@marularch 10 ай бұрын
@@joshuahoem8740 I'm having the same issue just after adding the inventory items to the database, might be about Unity version (I'm using 2022.3.10f1)
@chris_hass
@chris_hass 3 жыл бұрын
i have error, when i try to build and run i got two errors, one says: Assets\scripts\inventory\Scriptable objects\Inventory\Scripts\InventoryObject.cs(21,13): error CS0103: The name 'databse' does not exist in the current context and second says: Error building Player because scripts had compiler errors
@hxckid
@hxckid 3 жыл бұрын
Check that you added database in Editor to InventoryObject component.
@TheRealspoingis
@TheRealspoingis Ай бұрын
Hey So I just cant seem to find out how to save Scriptable Objects that are instanciated in a scene what is the best type of inventory system that allows the same item multiple times with different stats in a players inventory thats saveable?? I have been using scriptable objects for the longest time since it allows for just creating the object over again and allowing to change the stats of those items but to make it so it saves feels impossible I Use CreateInstance does this work with this??
@ZukuseiStudios
@ZukuseiStudios 4 жыл бұрын
Great videos, well structured, thank you
@CodingWithUnity
@CodingWithUnity 4 жыл бұрын
np, and thank you :)
@zyrxom
@zyrxom Жыл бұрын
so after i built the game the inventory just stopped woking and I have no idea why it doesn't even work from the inspector
@marularch
@marularch 10 ай бұрын
I'm wondering if this tutorial is still relevant today or there are better ways of saving/loading data with scriptable objects. I hear a lot about addressables, would it be a better solution than this?
@thebulletkin8393
@thebulletkin8393 7 ай бұрын
Been working through this, but inn getting an issue that I simply cannot figure out the reason behind. Essentially, when I boot up the first scene everything works fine. On load it will add the item slots and game objects to the dictionary and update it as expected. On the second scene this works too. But when I go from the second screen to the first screen again, through breakpoints I can see that on load it correctly assigns the slots to game objects. But between the scene loading and me picking up a new item, the values in the dictionary just become null. Have you ever encountered an issue like that with this setup?
@RedShedG
@RedShedG 3 жыл бұрын
I exported the project and they everything broke, i can only collect if i have the item allready, save doesn't work anymore, and so on :( and it's not only in the build, it's also in the preview
@drake_eric9799
@drake_eric9799 5 ай бұрын
i build it and run load is not working database is keep reseting to no database
@tinhonlien
@tinhonlien 3 жыл бұрын
sory I want to ask Why when I build the pc, and run the program it does not show the item in the inventory but in the unity editor run ok
@SineWav0101
@SineWav0101 3 жыл бұрын
This has been a great tutorial so far but I'm still receiving the KeyNotFoundException error. I've changed the script execution order to ensure the ItemDatabase script's OnAfterDeserialize() callback is called first and added checks to the InventoryObject but still receiving the same error. The ItemDatabase script is simply not recognizing the object key. Any suggestions?
@dattoproduction4450
@dattoproduction4450 3 жыл бұрын
same. have you fixed it yet?
@chosenone3
@chosenone3 3 жыл бұрын
​@@dattoproduction4450 @Bryan Lopez You must have put the code: getId = new Dictionary(); getItem = new Dictionary(); for (int i = 0; i < items.Length; i++) { getId.Add(items[i], i); getItem.Add(i, items[i]); } into the OnBeforeSerialize function instead of OnAfterDeserialize. Also, make sure this code: for (int i = 0; i < container.Count; i++) container[i].item = database.getItem[container[i].id]; is also in OnAfterDeserialize.
@eleocraft278
@eleocraft278 2 жыл бұрын
@@chosenone3 Doesn't works, OnBeforeSerialize is not necessarely called before OnAfterDeserialize
@connorbrush5626
@connorbrush5626 4 жыл бұрын
When I load in my build before saving in my build, it does not load the save from my editor. It instead spams my debug log with errors and infinitely instantiates inventory slots. What could be the error? How could I fix?
@CodingWithUnity
@CodingWithUnity 4 жыл бұрын
It sounds like some code is in the wrong spot, but switching ISeralizationCallback with Onvalidate might help
@antonclark608
@antonclark608 3 жыл бұрын
I encountered this issue as well. The reason it was happening was because I had the [System.Serializable] tag in front of the database variable. But the same issue would occur if it was public. Just make sure that the variable is private and cannot be read by unity.
@garypowell3566
@garypowell3566 4 жыл бұрын
Can you help please...So far, tutorial going well, except for the Save & Load which is not working at all. I have tried assigning different Key Codes, but it's just not happening. Any suggestions/help would be appreciated thanks.
@CodingWithUnity
@CodingWithUnity 4 жыл бұрын
I think i saw you got it working, correct?
@garypowell3566
@garypowell3566 4 жыл бұрын
Yes I did thanks
@Thundorn
@Thundorn Жыл бұрын
is there not a method we can add to the ItemObject which automatically appends itself to the database when it's created so that we don't have to manually add each item? I would like to make it more designer friendly.
@drake_eric9799
@drake_eric9799 5 ай бұрын
help pls build and run is to long loading maybe i skip making folder to other asset script
@seanballard2546
@seanballard2546 2 жыл бұрын
ok so this is a old video but ive followed everything perfectly but i cant get the save and load from pressing "space" and "enter" to work. i do have a character controller that uses the "GetKey("Space") for jumping in my character controller so would that be the problem?
@excuses7032
@excuses7032 4 жыл бұрын
Hey so ive been recently following this tutorial but it always brings up an error with the resource folder when trying to build. any chance things changed since then?
@CodingWithUnity
@CodingWithUnity 4 жыл бұрын
Yes, I get rid of the resource folder around video 4 I believe, cant remember for sure. But it doesn't stay long and i start doing build test at the end of the videos in one of the future videos on
@excuses7032
@excuses7032 4 жыл бұрын
@@CodingWithUnity Thank you so much. Especially for responding so quickly. Your tutorials have been amazing so far and cant wait to get through the rest.
@CodingWithUnity
@CodingWithUnity 4 жыл бұрын
@@excuses7032 No problem! :)
@qiangpanchen6785
@qiangpanchen6785 2 жыл бұрын
Super Duper good! Love your tutorial ~
@unitygametv7406
@unitygametv7406 Жыл бұрын
Invalid operation exception. You are trying to read input using the UnityEngine.Input class, but you have switched active input handling to input system. Why this error ?
@MattRaffel
@MattRaffel 4 жыл бұрын
Can't you use System.IO.Path.Combine() in place of the string.concat functions and it automatically handles directory (folder) separators for different operating systems? you should also wrap the FileStream with using() so that it automatically handles resource clean up on exceptions.
@CodingWithUnity
@CodingWithUnity 4 жыл бұрын
Both good ideas, didnt even cross my mind! Thanks!
@magictimm4090
@magictimm4090 4 жыл бұрын
Hi Matt. could you please tell me the line of code? I cannot handle it. FileStream file = System.IO.Path.Combine(Application.persistentDataPath, saveData)); is not correct
@leonmulthaupt6430
@leonmulthaupt6430 2 жыл бұрын
@@magictimm4090 I *think* he put "using System.IO;" at the top of the InventoryObject.cs with just using a rightclick and a menu and never scrolled up to the beginning of the code so you might have missed it. I only know cause I missed it, too, and typed it in manually.
@joshuadecator6416
@joshuadecator6416 4 жыл бұрын
Everything seems to be working besides when i go into build i no longer can pick up the items, but the save and load works :)
@drake_eric9799
@drake_eric9799 5 ай бұрын
space and enter not working but other button working
@channeltrash6257
@channeltrash6257 2 жыл бұрын
how can i save multiple databases? cuz i can create worlds, several worlds in which the save path is different. so if i'm in the "New world 1" that's my save path and the game loads my data which is saved in the "New World 1" folder.
@glenzhang3646
@glenzhang3646 4 жыл бұрын
very good stuff, it'll be even better if you can explain more XD
@whynot2349
@whynot2349 Жыл бұрын
Really amazing tutorial!!! tysm for making the effort:D, I just had one question: when saving for example my inventory with 2 items inside, then picking up another item and loading, all 3 items show up, not only the 2 saved ones. Is that meant to happen?
@masterlilnut6625
@masterlilnut6625 Жыл бұрын
22.14 what is the point of having the first database selection and not just always using the second one since it works on build
@masterlilnut6625
@masterlilnut6625 Жыл бұрын
i meant to say at 22:14 like the timesstamp
@chrisflynn5192
@chrisflynn5192 4 жыл бұрын
Hey there, loving these tutorials. I've made an inventory before but not had much experience with scriptable objects. Loving them already though, and they seem straight forward so far. I did have a query regarding your database though. Why do you need the dictionary? If you are populating the ItemObject[] in the editor and you make sure that the ItemObject.id is the same as its index in the array surely you can access it directly using item[id]. You could then just store the database object in your resources folder. Am I missing something here?
@kamikaisan
@kamikaisan Жыл бұрын
i totally agree with you, and that was exactly my approuch, but with a few changes, since i'm not leaving the item array and the dictionary, which i named mappedIds, as public, i made a method for the DatabaseObject: GetItem(int id) { return item[id]; } and another for GetId(ItemObject item) { return mappedIds[item]; }
@freshedpotato
@freshedpotato Жыл бұрын
I dont think my last comment went thru so I'll do it again, for some reason when I play the game ( not when I quit but when I play ) the container and inventorysave gets cleared and therefore I can't load it. Edit: nvm I fixed it, it was some other code I wrote and forgot about a while ago...
@ljhoser5842
@ljhoser5842 4 жыл бұрын
I am getting an error that says keynotfoundexception, it says line 14 and 35, but there doesn't seem to be an issue in those lines? (it happens when the player touches the item)
@CodingWithUnity
@CodingWithUnity 4 жыл бұрын
it probably is not finding the item in your item database
@danielsalyi7194
@danielsalyi7194 4 жыл бұрын
Try this: In the for loop for get item, Write: If(database. Get item. Containskey(container[I]. ID) //populate that item
@NightmarezTerror
@NightmarezTerror 3 жыл бұрын
Well otherwise working but..." error CS0103: The name `AssetDatabase' does not exist in the current context "
@stylaender
@stylaender 3 ай бұрын
private void OnEnable() { #if UNITY_EDITOR database = (ItemDatabaseObject)UnityEditor.AssetDatabase.LoadAssetAtPath(".../Items/Database.asset", typeof(ItemDatabaseObject)); #else database = Resources.Load("Database"); #endif }
@NightmarezTerror
@NightmarezTerror 3 ай бұрын
@@stylaender I might check this or not (I have moved to other attempts at inventory lol)
@PerBonomi
@PerBonomi 2 күн бұрын
@@stylaender perfect, thank you!
@vergilw7009
@vergilw7009 2 жыл бұрын
Hello, I`m new to Unity , I want to ask if add an Item_ID on the Item Scriptable Objects, then put each Item an unique ID, is it work for not adding the database section ??
@nevimkde9537
@nevimkde9537 3 жыл бұрын
Awesome series, it is for me so complicated, even throught the fact, that I am into programming for 2 years (I know its nothing but still). Only one question and that is at 13:35, how exactly did you attached FileSteam to that string???
@ryanbeattie9170
@ryanbeattie9170 3 жыл бұрын
Not sure if this is what u might be looking for but, just curious if u added the using System.IO; at the top. Plus the save path was is supposed to be typed in by you on the inventory scriptable object inside the inspector. Will only work if that scriptable object is in that location. Hope this was what u were looking for or talking about.
@itsnotthatsimple8852
@itsnotthatsimple8852 3 жыл бұрын
Thank you so much for the series. SO great. :) 18 months after posting it turns out that Unity 2020.2 does not allow namespace changes using #if. So the fix you apply at 20:50 is no longer valid. You don't happen to have an alternate fix for saving/loading the database?
@itsnotthatsimple8852
@itsnotthatsimple8852 3 жыл бұрын
Just had a look at the GitHub and realized you made big changes later on. If the issue persists and you have a fix feel free to shout out. I'll keep on monkeying after the tutorial and perhaps it'll take care of it self. Thanks again!
@CodingWithUnity
@CodingWithUnity 3 жыл бұрын
A bit confused, I didn't do a namespace change with that If def
@itsnotthatsimple8852
@itsnotthatsimple8852 3 жыл бұрын
@@CodingWithUnity Definitely my confusion. "#if UNITY_EDITOR" is no longer available in 2020.2. Guess I've got my terms mixed up. I thought of that as a namespace change. No matter - it quickly disappeared in part 3.
@ahmadtaib6199
@ahmadtaib6199 Жыл бұрын
how to save using cloud storage like cloudonce? did anyone know? please im stuck around 5 month right now for android
@thunderhawk4592
@thunderhawk4592 2 жыл бұрын
Great tutorial so far but if i use #else in onenable causes errors if i dont have everything is fine Edit- if #else doesnt work for u use #if UNITY_EDITOR database = (ItemDataBaseObject)AssetDatabase.LoadAssetAtPath("Assets/Resorces/New Item Database.asset",typeof(ItemDataBaseObject)); #endif #if UNITY_STANDALONE_WIN database = Resources.Load("New Item Database"); #endif u might need to change the standalone depending on what ur doing
@stylaender
@stylaender 3 ай бұрын
private void OnEnable() { #if UNITY_EDITOR database = (ItemDatabaseObject)UnityEditor.AssetDatabase.LoadAssetAtPath(".../Items/Database.asset", typeof(ItemDatabaseObject)); #else database = Resources.Load("Database"); #endif }
@jessnelson1168
@jessnelson1168 2 жыл бұрын
Love these tutorials! Question: does this code work if you're building an android app?
@CodingWithUnity
@CodingWithUnity 2 жыл бұрын
Can't think of any reason it would not, might have some path issues with the saving if I didn't use persistantDataPath but I think I did
@GameDevByChill
@GameDevByChill 3 жыл бұрын
Instead of adding another dictionary i decided to go with Linq to get the ItemObject by Id as so... public void OnAfterDeserialize() { for (int i = 0; i < container.Count; i++) { var keys = database.Get.Where(x => x.Value == container[i].id).Select(x => x.Key); container[i].item = keys.FirstOrDefault(); } } I havent done any performance comparisons with this as its just an idea i had. :)
@eduarddebruyn4841
@eduarddebruyn4841 3 жыл бұрын
Your videos are so great and so helpful!! However I seem to have an issue. When changing the ID like you do at 11:41 my Item does not update? I have followed your examples to the letter... Please can you assist?
@eduarddebruyn4841
@eduarddebruyn4841 3 жыл бұрын
Figured it out, never had ISerializationCallbackReceiver on my InventoryObject.cs
@CodingWithUnity
@CodingWithUnity 3 жыл бұрын
Good job fixing it!
@Ineedgames
@Ineedgames 3 жыл бұрын
Having trouble with a line of code. When I go to pick up the items, I get a NullReferenceException for Container.Add(new InventorySlot(database.GetId[_item], _item, _amount));
@dinendash1166
@dinendash1166 3 жыл бұрын
public void AddItem(ItemObject _item, int _amount) { for (int i = 0; i < Container.Count; i++) { if (Container[i].item == _item) { Container[i].AddAmount(_amount); return; } } { Container.Add(new InventorySlot(dataBase.GetId[_item], _item, _amount)); }
@X-Ter
@X-Ter 3 жыл бұрын
​@@dinendash1166 have you set the database object in the inventory scriptable object?
@gamesarelegends3473
@gamesarelegends3473 4 жыл бұрын
What did you clicking at the FileStream at 13:48? I dont have Visual Studio so I dont know what you activated.
@CodingWithUnity
@CodingWithUnity 4 жыл бұрын
i cant see anythignt hat i clicked, just that i used string.concat (which for reference, its better to do `$"my concated strings number is {myInt}"`
@philberex
@philberex 3 жыл бұрын
Thank u for this wonderfull tutorial.
@danielsalyi7194
@danielsalyi7194 4 жыл бұрын
Could you please help me? Inventory works, even if I quit and reload the game. But if I update my app(mobile game), it cannot repopulate the items. Amount of the items are correct but it doesn't recognise what item it is. Is there a work around for this? I tried to use an array, but it break the system and loading didn't work. I am guessing the reference from the dictionaries break => cannot repopulate Or the database reference breaks and no loading (still using the resource system)
@chrisflynn5192
@chrisflynn5192 4 жыл бұрын
It definitely sounds like your database reference is the issue to me. If it is then none of your ItemObject assets will be included in your build. Are you loading your database using Resources.Load?
@helpmegame
@helpmegame 4 жыл бұрын
Couldn't you use [SerializeField] above your ItemDatabaseObject variable declaration at 19:00 instead of the OnEnable? It would allow you to still set and change the value through the editor and may work better. I'd think it would make things smoother if you moved paths around.
@Sekunri
@Sekunri 3 жыл бұрын
His goal was to keep the database Deserialized so using SerializeField would have rendered the point moot.
@nevimkde9537
@nevimkde9537 3 жыл бұрын
I know, you may not read all comments, especially ones, who has been written on 2 year old video, but if you wil read this, I just wanted to ask you one question and that is, how long had it taken to create such an inventory, if it was in spare of days, or even longer, because with my programming expierence, I will never be able to do such think. Also did you did this by yourself, or you had to google a bit?
@CodingWithUnity
@CodingWithUnity 3 жыл бұрын
If I wasn't making the videos along with it I'd say, 25-50 hours of work give or take and I did it by myself but sure I had to google sometimes. Although this is not the first inventory system iv done. I'm sure you could do it also, just takes practice
@alfredoheights
@alfredoheights Жыл бұрын
Why do you need the #if UNITY_EDITOR block? Why not just load from the resources folder in the editor? Great tutorial by the way, definitely using this.
@darrylvillanueva5233
@darrylvillanueva5233 2 жыл бұрын
i have facing a problem on Load function when i put the load function on OnDisable function all the save data are deleted! can someone help me please? Thank you!!
Unity3D - Item Buffs/Stats |  Scriptable Object Inventory System | Part 3
29:25
Unity3D - Scriptable Object Inventory System | Part 1
31:31
Coding With Unity
Рет қаралды 327 М.
When you discover a family secret
00:59
im_siowei
Рет қаралды 15 МЛН
PEDRO PEDRO INSIDEOUT
00:10
MOOMOO STUDIO [무무 스튜디오]
Рет қаралды 16 МЛН
Scriptable Objects: What are they? How do you use them?
10:38
Unity 2021 Use Scriptable Object instead of Enum
10:57
Sunny Valley Studio
Рет қаралды 37 М.
Creating An Inventory System in Unity
11:03
Game Dev Guide
Рет қаралды 178 М.
Be CAREFUL with Scriptable Objects!
8:27
Code Monkey
Рет қаралды 79 М.
20 Advanced Coding Tips For Big Unity Projects
22:23
Tesseract
Рет қаралды 179 М.
Rethink Everything with Scriptable Object VARIABLES
15:28
When you discover a family secret
00:59
im_siowei
Рет қаралды 15 МЛН