- Home /
Accessing a private var from a subclass
How do I access a private var that is defined by a superclass. I believe other languages usually have a protected
as well as public
and private
that allows this sort of thing.
class Foo extends MonoBehaviour { private var someVar:int = 10; }
class Bar extends Foo { function Update() { Debug.Log(someVar); } }
Output:
'Foo.someVar' is inaccessible due to its protection level.
I don't want this var showing up in the unity object inspector, but I do want subclasses to easily access it.
Do I need to create an explicit getter function getSomeVar()
for this? That seems inelegant.
Answer by TheMPC · Apr 20, 2010 at 06:55 PM
If im not mistaken, you can try this:
class Foo extends MonoBehaviour { protected var someVar:int = 10; }
class Bar extends Foo { function Update() { Debug.Log(someVar); } }
A protected var is not accesable for all classes, only accesable for the class that has the protected var and all classes that extends the class that has the protected var.
Ha. You know, I tried typing that and it didn't highlight, so I thought the keyword doesn't exist. I'm betting I just misspelled it. /slaps forehead.
Answer by Molix · Apr 20, 2010 at 07:01 PM
Even for public members you can add the HideInInspector tag, e.g.:
@HideInInspector
public var imPublicButNotInInspector : int;