- Home /
Public field Range() not working inside ScriptableObject
I made a sound class with ranges on values, but trying to make a data file out of them just ignores the range() attribute completely.
[System.Serializable]
public class Sound {
public string name;
public AudioClip clip;
[Range(0f, 1f)]
public float volume = 1f;
[Range(0.5f, 1.5f)]
public float pitch = 1f;
[Range(0f, 1.5f)]
public float randomVolume = 0f;
[Range(0f, 1.5f)]
public float randomPitch = 0f;
}
and here is my tiny ScriptableObject:
[CreateAssetMenu(fileName = "SoundData", menuName = "SoundData", order = 1)]
public class SoundData : ScriptableObject {
[SerializeField]
public Sound[] sounds;
}
What ends up happening is that I don't see the range slider at all. This used to work while I just had the data inside the audio manager instead of a scriptableobject.
Is there a way to fix this?
I'm not sure if I like the sound of that, but... thanks for the info!
Are you sure you don't have any custom editor for Sound
or SoundData
?
I have no custom editors, and looking at the sound element array when fetched into the Sound$$anonymous$$anager works just fine.
Answer by Bunny83 · May 30, 2018 at 10:27 AM
Sorry but i can't reproduce this problem.
I've copied your exact code into my two test projects and it shows up like this. I've tested with Unity 2018.1.0b11 (beta version) as well as with my Unity 5.6.1f1 version. Both look like in the image i've posted.
You haven't mentioned your Unity version or if you have tried it in a new empty project. For me it works as it should. So there has to be something else which you did not mention. Any PropertyDrawers? Do you use Assembly Definition Files? Do you use namespaces? Are the two classes in seperate files or in the same? Does the filename of the scriptfile that contains the SoundData class match SoundData.cs? Do you use actual scriptfiles in your project or do you use pre-compiled assemblies?
I'm not an experienced Unity user. SoundData and Sound are in the same file (separating them did nothing), I haven't used PropertyDrawers or Assembly Definition Files, I don't use namespaces (I'm a rather new C# dev, basically my first personal project), and now the SoundData file is called SoundDataClass.cs. And I use actual script files made in the unity editor.
SoundData
should be defined in a file called SoundData.cs
.
Because Sound
is a plain C# class, you can define it in the same file as SoundData
if you want, or in a file called Sound.cs
To add some context here: It is a quirk of Unity that $$anonymous$$onoBehaviour- and ScriptableObject-derived classes must be defined in a file with the same name as the class. It's pretty non-obvious in the UI, but the indicator here is the Script field that has nothing assigned. When it is done properly, it should have the $$anonymous$$onoScript asset that defines the class. Basically, Unity has created an object with the same serialization layout you have specified, but it doesn't actually know what it is.
Defining it in SoundData.cs did nothing, and neither did defining Sound in Sound.cs or SoundData.cs.
Never $$anonymous$$d, I had to recreate the data asset, then it worked. Thank you for the help. :)
Ok, so for actually trying the provided classes in two different Unity versions, verifying that it does work as expected, providing a screenshot of the result i get a downvote? Well, i guess I take another break from answering questions... have a nice day debugging...
Don't know who downvoted you and converted my comment to an answer, but your answer was the right one.
Sorry. I converted @Hellium's comment to an answer because I thought it made it easier to identify the specific solution to this problem, so future readers wouldn't have to look through the whole thread unnecessarily to find it.
It also looks like I accidentally down-voted this thread. I had up-voted it but then decided to up-vote the new answer ins$$anonymous$$d. I clicked down on this thread to cancel my up-vote ins$$anonymous$$d of re-clicking the up-vote to toggle it off. Sorry for the confusion.
No idea who downvoted, but thanks for the help. Your answer was helpful. :)
Answer by c8theino · Jun 16, 2020 at 10:48 AM
I found one possible reason why this could happen. It seems that if you have your Inspector in "Debug" mode, the sliders are not working. Change back to "Normal" mode and everything should work just fine.
Your answer
Follow this Question
Related Questions
[Range] attribute saves to undo-history too often. 1 Answer
Editing custom types in the inspector 1 Answer
Add AudioSource-Like Range to Script 1 Answer
Editor: How to save changes to a ScriptableObject loaded from Resources? 1 Answer
can't see property of asset created from scriptable object 2 Answers