I don’t think you can modify an attribute at runtime since they are part of the metadata. You can access them but modifying them should be done at class level not instance (I could be wrong though).
You may want to use a property instead and some variables.
private int _rangeMax;
private int _rangeMin;
private int _width;
public int RangeMin{
get;
set{
_rangeMin = value;
Width = _width;
}
}
public int RangeMax{
get;
set{
_rangeMax = value;
Width = _width;
}
}
public int Width {
get{return _width;}
set{
_width = Mathf.Clamp(value, rangeMin, rangeMax);
}
}
Thanks very much for the example code. I'm sorry to be slow, but I'm looking at it over and over and I can't see how the int slider for width in the Editor will become restricted to the range? It just seems to be setting/getting some new unrelated ints. How does this relate to my SerializedProperty ? Again,sorry for not seeing it, but I seem to be missing a step, where _width becomes related to width.
It does not. If I understood right you want to modify the value at runtime and there is no inspector at runtime (at least when you build). So I thought the point was to set the value based on some other behaviours. Here you have your width that is set based on rangeMin and rangeMax. If you change one of the last two, then width is reset automatically and this is at runtime even after creating the build.
OK, then even without involving the inspector, could you demonstrate how calling RangeMin(), RangeMax() or Width() properties can influence the value of width (not _width) at any point. I'm coming from C++, so I just re-watched the tutorials on properties. I haven't yet seen any mention of how naming a new variable with an underscore will relate it to the real variable. Did you mean to write 'width' (no underscore) ?
Thanks very much for the example code. I'm sorry to be slow, but I'm looking at it over and over and I can't see how the int slider for width in the Editor will become restricted to the range? It just seems to be setting/getting some new unrelated ints. How does this relate to my SerializedProperty ? Again,sorry for not seeing it, but I seem to be missing a step, where _width becomes related to width.
– JoeW97It does not. If I understood right you want to modify the value at runtime and there is no inspector at runtime (at least when you build). So I thought the point was to set the value based on some other behaviours. Here you have your width that is set based on rangeMin and rangeMax. If you change one of the last two, then width is reset automatically and this is at runtime even after creating the build.
– fafaseOK, then even without involving the inspector, could you demonstrate how calling RangeMin(), RangeMax() or Width() properties can influence the value of width (not _width) at any point. I'm coming from C++, so I just re-watched the tutorials on properties. I haven't yet seen any mention of how naming a new variable with an underscore will relate it to the real variable. Did you mean to write 'width' (no underscore) ?
– JoeW97Thanks very much for the explanation. I see now how using those properties could solve a general problem of enforcing a range upon a variable. Sorry, my OP doesn't explain why I can't go this route. I edited my question to explain my dilemma better, but it changed so much I thought it was better as a new question: [http://answers.unity3d.com/questions/586032/how-to-create-an-undo-able-slider-in-a-custom-edit.html][1] [1]: http://answers.unity3d.com/questions/586032/how-to-create-an-undo-able-slider-in-a-custom-edit.html
– JoeW97