Exposing Member Variables of a struct to the inspector

I’ve created the following struct

struct Sounds
        {
            public AudioClip MainEngine;
            public AudioClip LevelComplete;
            public AudioClip Death;
            public AudioClip BonusItem;

            public void AssignSounds(AudioClip[] sounds)
            {
                MainEngine=sounds[0];
                LevelComplete= sounds[1];
                Death= sounds[2];
                BonusItem= sounds[3];
            }
        }

I would like to expose the variables on the inspector and do away with the AudioClip and construcctor so that the struct is constructed on the inspector.

What I’ve Tried
[SerializedField] Sounds sounds;

[SerializedField] struct Sounds {...}

struct Sounds
    {
        [SerializedField] AudioClip MainEngine;
        [SerializedField] AudioClip LevelComplete;
        [SerializedField] AudioClip Death;
        [SerializedField] AudioClip BonusItem;
    }

What I Hoped for was to see the variables displayed on the inspector similar to how an Array is displayed. What I got was “nothing”.

I apparently don’t even know how to ask the search engine to find the right scripting reference as it came back with

If someone could point me towards the answer I’d appreciate it.

I will just throw this, at the end he seems to have exposed it but I didnt watch the whole thing

Interesting approach, thanks for finding that Bryant. Sadly, it wasnt quite what I was looking for but still learned something new so not a loss.

For those interested you can take a look at the working script (Rocket.cs) here:

I am pretty sure that the answer to expose a struct is

 [System.Serializable]
 public struct YourStuct
 {
    public int index;
    public bool isSet;
 }

dont remember if you also need to add the [SerailzeField] to the member fields. I dont think you do but you can experiment.

https://docs.unity3d.com/ScriptReference/Serializable.html

Ya I just pulled up my code to check since I just recently exposed a class and here is my code…

[System.Serializable]
public class Waypoint
{
  public Vector3 destination;
  public AnimationCurve easing;
  public float desiredTime;
  public bool useDefaultEasing;

  public Waypoint(Vector3 position)
  {
    destination = position;
    easing = null;
    desiredTime = 1f;
    useDefaultEasing = true;
  }
}

Here is what it looks like in unity… It was a struct until earlier tonight when I realized I did not want it to be a value type.

Thank You mbalrog6. I’m pretty sure I tried something similar at some point and it didn’t work but this time it worked just like I wanted. Obviously, I must have messed up the syntax before so your sample code is much appreciated:+1:

Virgel
Refactored Code

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms