Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Properly saving and loading an item.

Discussion in 'Getting Started' started by FireMaster5431, Jun 14, 2023.

  1. FireMaster5431

    FireMaster5431

    Joined:
    Jan 5, 2022
    Posts:
    2
    I created a level saving system where all the items in the level gets saved turned into a json string just using a for loop.

    I have just had some problem in figuring out how to properly and easily load an item, where every item have a different script inside it and it should save different values (ex: one item have a script called "hello there" where "id" value should be loaded and another have a script called "general kenobi" where "general grievous" value should be loaded). Should i just have a long switch statement witch each obj_id inside it and do a different action from each? or is there a more proper way to do it?

    if youre wondering this is the JSON i get for each item: {"name":"Box (1)","position":{"x":2.0,"y":6.5,"z":3.0},"rotation":{"x":0.0,"y":0.0,"z":0.0},"scale":{"x":1.0,"y":1.0,"z":1.0},"color":{"r":0.0,"g":0.8793842792510986,"b":1.0,"a":1.0},"obj_id":3,"id":"","_static":true}
     
  2. AngryProgrammer

    AngryProgrammer

    Joined:
    Jun 4, 2019
    Posts:
    490
    Unity model have something like prefabs and variants. You can make a ScriptableObject that inside will have a list or a dictionary of prefabs. So you load the data, translate id from json to the prefab, instantiate it, and add rest of data like position.
     
  3. FireMaster5431

    FireMaster5431

    Joined:
    Jan 5, 2022
    Posts:
    2
    Yes that is exactly what i am doing, the problem is that for exampel, i have a button, that script has a couple of variables that should be saved and loaded, and then i have a door, that should have different ones.