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. Dismiss Notice

Multiple Data Types within an Array Element?

Discussion in 'Scripting' started by Tsondre, Apr 9, 2016.

  1. Tsondre

    Tsondre

    Joined:
    Apr 9, 2016
    Posts:
    30
    Is there a way to have multiple data types (bool, string, float, int) within a single Array Element? I would also want this array to be public so that I may modify variables in the editor.

    Here the best example of what end result I'm looking for:

    variables I'm working with:

    AudioSource audio;
    float audioVolume;
    bool playOnce;


    AudioOptions
    Element 0
    audio
    audioVolume
    playOnce
    Element 1
    audio
    audioVolume
    playOnce
    Element 2
    audio
    audioVolume
    playOnce
     
  2. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    Make a struct that contains audio, audioVolume, and playOnce, then make an array of those structs.
     
  3. Tsondre

    Tsondre

    Joined:
    Apr 9, 2016
    Posts:
    30
    Thank you for the help Garth. I made the struct into an array but it didn't show in the inspector. I found out that I have to use [System.Serializable] right above my struct to get it to work.
     
    GarthSmith likes this.