6
\$\begingroup\$

I'm trying to convert a javascript file to a C# script for Unity, mostly because the rest of my workflow is in C#, and I'm much more comfortable with C#. In this case, it's an audio controller script.

The .js looks like this:

var audioClips              : AudioClips[];                     // Array of single use audio clips
var audioLoops              : AudioClips[];                     // Array of loops

class AudioClips{
    var name            : String;
    var audioClips      : AudioClip[];
    var volume          : float             = 1.0;
    var volumeBySpeed   : boolean           = false;
    var minVolume       : float             = 0.2;
    var pitchBySpeed    : boolean           = false;
    var minPitch        : float             = 1.0;
    var maxPitch        : float             = 1.0;
}

and the editor looks like this:

enter image description here

My C# script looks like this:

public class AudioClips : MonoBehaviour
{
    public string       _name;
    public AudioClip[]  audioClips;
    public float        volume          = 1.0f;
    public bool         volumeBySpeed   = false;
    public float        minVolume       = 0.2f;
    public bool         pitchBySpeed    = false;
    public float        minPitch        = 1.0f;
    public float        maxPitch        = 1.0f;
}

public class AudioController_SFB_C : MonoBehaviour {

    public AudioClips[] audioClipsArray;                        // Array of single use audio clips
    public AudioClips[] audioLoopsArray;                        // Array of loops

and the editor looks like this:

enter image description here

The C# code doesn't reflect all of the public variables. Interestingly, even if I comment out the public AudioClip[] section and the code that relies on it, my inspector looks the same. I'm thinking maybe there a scoping issue or a strong-typing issue that I'm missing, but I can't seem to see it.

\$\endgroup\$
7
\$\begingroup\$

Your missing data from the included instance of AudioClips. UnityScript is a little more flexible, in that it already serialises these things for you. In C#, you need to specify that you intend to use serialisation, with the [System.Serializable] attribute.

As a side note, you have included the inheritance of AudioClips : MonoBehaviour. We need to expressly note inheritance in C#, where UnityScript correctly assumes. However, this particular class is being used to store data, not to represent a mono behaviour. We should not inherit from MonoBehaviour.

Simply remove the inheritance and add the attribute to your class, as follows, and it should display in the editor.

[System.Serializeable]
public class AudioClips
{
    public string _name;
    public AudioClip[] audioClips;
    public float volume = 1.0f;
    public bool volumeBySpeed = false;
    public float minVolume = 0.2f;
    public bool pitchBySpeed = false;
    public float minPitch = 1.0f;
    public float maxPitch = 1.0f;
}
\$\endgroup\$
  • 1
    \$\begingroup\$ That was 99.5% of it - thank you. Can't even believe I missed that. The other part was then not leaving it as a MonoBehavior. I had initially set it as a MonoBehavior because it was the only thing that made it show up at all. \$\endgroup\$ – Jesse Williams Nov 19 '16 at 22:32
  • 1
    \$\begingroup\$ @Jesse Williams, of course. I completely missed that, myself. Updating my answer, thanks for the amendment. \$\endgroup\$ – Gnemlock Nov 20 '16 at 0:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.