- Home /
Use Another Float Value As Range Attribute's Max Value
Hi guys,
I want to use a float value to define the max range of the Range attribute.
For example,
float a = 4.0f;
[Range(0.1f, a)]
public float b;
Is this possible?
Thanks!
Hi, yeah I am: CS0120. It's saying An object reference is required for the non-static field.
Answer by Kim-Nobre · May 15, 2019 at 03:50 PM
You can do this by using the "const" keyword in the "a" field.
const float a = 4.0f;
[Range(0.1f, a)]
public float b;
Hi, I tried this and it's compiled, but its removed the fields in the Inspector. I've tired to serialise it, to no avail. Any ideas?
I think there's an implication in the question that the OP wants to be able to change the allowed range. Otherwise... why bother?
$$anonymous$$aking the range depend on a const just changes where a fixed range is defined, doesn't make it adaptive in any way at all.
@TSCSimulation_AH I wouldn't have thought it possible to do what I'm assu$$anonymous$$g that you're asking for without writing your own editor code. But should be quite possible in a custom editor or property drawer.
Hi, yeah, I wanted to just make it so that the range of one float could never be higher than the other, so if I set a to be 400 b could only ever be 0 - 400, for example, as I've got $$anonymous$$/max logic elsewhere. It's just a nice to have for the rest of the $$anonymous$$m so mistakes aren't made so easily, if it's not possible, it's not the end of the world.
Like I said, I'm sure it is possible by writing your own editor. I've not done this specifically, by have done similar things such as making the options in an inspector dropdown for an asset be deter$$anonymous$$ed by the contents of an array in another asset. If you're up to speed on writing basic property drawers and inspectors then my guess is that what you're asking for would be pretty straightforward. Possibly the fiddliest bit would be dealing with the situation where things get broken (eg you'd need to think about what you want to happen when the range-deter$$anonymous$$ing variable gets changed to something that means the range-limited variable is now out of range - do you allow that? do you automatically clamp the range-limited variable? do you need to message the user that these kind of things have happened? and so on)