|
I've got a class like this: And so when I attach this to a GameObject, the default value displayed is 5. Then, let's say I have a subclass where I would like the default value of this variable to be some value other than 5. The above does not work, but is there a way to get this sort of behavior? My particular case is that each of several subclasses share a "speed" variable, but they each require this value to be different by default. Thanks!
(comments are locked)
|
|
Yes you can.. ... Excellent. This is the answer. Thanks. For anyone else checking out this question, the Reset() function is built in MonoBehaviour function that is called "when adding the component the first time". So you can initialize the values here.
Oct 04 '12 at 05:21 AM
jwinn
Swapped the answer because this seems to be exactly the functionality to enable this. Looks like Reset maps pretty well to the "defaultproperties" block in Unrealscript (if anyone cares :P)
Oct 05 '12 at 04:19 AM
kromenak
(comments are locked)
|
|
Subclasses have full inheritance, so you wouldnt need to declare it again. You can either assign it in a Start method as you would any other variable, or the easier way is to set its value in the inspector. (Note that private variables are not inherited). Hi, thanks for the reply! The problem though, is that if I just use inheritance as usual, the default value that shows up in the inspector for both scripts is 5. When I add the BaseClass component to an object, I want the default inspector value to be 5. When I add SubClass component to an object, I want the default inspector value to be 10. My main reasons for wanting to do things this way is to be able to take advantage of polymorphism to some degree by being able to access someValue without knowing what subclass it is, but I also want the default values in the inspector to be different for each subclass to avoid user error (if I know that the initial value for a class should always be 10, why leave it up to the user to switch it to this value?). And unfortunately, the "Start" method doesn't work because it will overwrite any changes that have been made in the inspector.
Aug 19 '11 at 06:18 PM
kromenak
Maybe I'm just misunderstanding you. In the inspector for the base class, you can leave it at 5. Then in the inspector for the subclass, you can change it from 5 to 10 (or whatever), and it will only apply that change to the object with the subclass script attached. Sorry if im not understanding your thought process.
Aug 19 '11 at 07:16 PM
djmorrsee
I might be misunderstanding myself, but the problem I was having was that if I attached the subclass to a GameObject, the initial value shown in the inspector is 5, the value I set in the BaseClass. Problem is, I would like to be able to set an initial value specifically for that subclass so that instead of displaying 5 initially, it would display 10.
Aug 19 '11 at 08:12 PM
kromenak
As far as I'm aware, subclasses will always initially use their parent's values, and you'll have to change it for each case that you need.
Aug 20 '11 at 03:25 PM
djmorrsee
(comments are locked)
|
