Search Unity

Question saving and loading random elements in a list

Discussion in 'Scripting' started by shadeburn, Jan 30, 2023.

  1. shadeburn

    shadeburn

    Joined:
    Jul 11, 2018
    Posts:
    2
    Hi I'm pretty new to c# I could really use some help.

    I made a list of gameobjects, and one gameobject is chosen by the Random.Range class.

    What I've been trying to do is save the randomly chosen gameobject and load it when the game is turned back on.

    Help is greatly appreciated.
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    There are many different ways to go about this. Look for a good saving and loading tutorial.
    But normally you're not saving the gameobject, you're saving data about the object. You haven't given much info, but let's just say your player has a weapon equiped. You don't save the gameobject for the weapon, you save the data for the weapon. How much ammo it has. What the weapon ID is, etc. So you can just repopulate and load up what you need to.
     
  3. TzuriTeshuba

    TzuriTeshuba

    Joined:
    Aug 6, 2019
    Posts:
    185
    you got the object the first time using an int that you obtained from random.range. just save that int, if your list's order isn't being jumbled. you can use PlayerPrefs.SetInt() and PlayerPrefs.GetInt() to persist data to harddrive between sessions
     
  4. shadeburn

    shadeburn

    Joined:
    Jul 11, 2018
    Posts:
    2
    nh
     
    Last edited: Feb 7, 2023