Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Serialize data in class using for loop?

Discussion in 'Scripting' started by kovata, Oct 31, 2015.

  1. kovata

    kovata

    Joined:
    Oct 24, 2015
    Posts:
    5
    Hi, I was watching live training session video about load/save data and have a question. In the training video the author created a serializable class like this:

    Code (CSharp):
    1. [Serializable]
    2.     class PlayerData
    3.     {
    4.         public int someparameter;
    5.  
    6.     }
    Is it possible to use for loop inside the [serializable] class? When I tried I received error message:
    "error CS1519: Unexpected symbol `for' in class, struct, or interface member declaration"
    I would like to build a for loop which go through a list of unknown number of gameobjects in it, find one parameter in every gameobject on that list and save it. How can I do it?
     
  2. Duugu

    Duugu

    Joined:
    May 23, 2015
    Posts:
    241
    Add an array or list of the desired data type to your class declaration and store the values in this array/list.
     
  3. kovata

    kovata

    Joined:
    Oct 24, 2015
    Posts:
    5
    Ahh, yes! Thank you.