- Home /
inheritance - instance variable problem?
Hi, I'm having a problem with my instance variables in an inherited class keeping their value. It's part of a larger problem I'm having, but I've tracked it down to this. Please help , thanks!
//subclass using UnityEngine; using System.Collections;
public class GUITab : GUIObjectTemplate {
public Texture2D texture_alt; public bool selected = false; public int test1 = 10;
// Use this for initialization
public override void Start () {
Debug.Log(test1); //should print 10, instead prints 0 : /
}
}
This is the full code for the inherited class, if it matters:
using UnityEngine; using System.Collections;
public class GUITab : GUIObjectTemplate {
public Texture2D texture_alt; public bool selected = false; public int test1 = 10;
// Use this for initialization
public override void Start () {
Debug.Log(test1);
}
// Update is called once per frame
void Update () {
}
//Constructor
public GUITab(Rect _area) {
area = _area;
}
public void Draw() {
if (MouseOver()) {
if (MouseDown()) {selected = true;}
}
if (selected || MouseOver()) {
GUI.DrawTexture(area, texture); //Null object crash
}
else {
GUI.DrawTexture(area, texture_alt); //Another null object crash
}
}
/*These methods are used for simplifying talking to the mouse*/
bool MouseOver() {
if (area.Contains(new Vector2(Input.mousePosition.x, Screen.height - Input.mousePosition.y))) {
return true;
}
return false;
}
bool MouseDown() {
if (Input.GetMouseButtonDown(0)) {return true;} return false;
}
}
Answer by Herman-Tulleken · Jul 29, 2011 at 05:25 PM
The problem is probably because that variable is set on the prefab or gameObject as 0 in the inspector. The assignment that goes with the declaration is some kind of default, and will be the value before it is changed in the inspector.
Usually, the correct thing to do is to change the value in the inspector to 0 (on the prefab or gameObject, as desired).
If you need to assign it from script, make the variable protected
, and set the value in Start
, like this:
public void Start()
{
test1 = 0;
}
That should also work for any inheritance scheme as expected.
Here is the full code:
class SuperClass : MonoBehaviour
{
protected int test1;
Start()
{
test1 = "12345"; //make it something that is easy to spot for testing
}
}
//-----
class SubClass : SuperClass
{
//do not redefine test1 here!
public void Start()
{
base.Start();
Debug.Log(test1);
}
}
Hm I've tried turning it into a private variable, which still won't work. It does work if I assign the variable in the inspector though : /
Strangely, both methods work in the superclass. The variable isn't being inherited from the superclass either.
Show us the superclass. And format the code in the question (highlight and press the 101010 button)
Whoops, I meant protected
, not private
. The code you posted as an answer (which you should not do, BTW, you should edit your original question!) cannot be correct, since you should not be able to define a public variable with the same name as one in a super class.
Remember also that your sub class Start
will override your super class Start
. If you do any initialisation in the super class Start
, you should call it from the sub class using base.Start()
.
Answer by conflictbliz · Jul 29, 2011 at 05:28 PM
also mouse should be Input.GetMouseButtonDown(0) or Input.GetMouseButtonup(0)
Answer by crump3ts · Jul 29, 2011 at 06:05 PM
Superclass:
using UnityEngine; using System.Collections;
public class GUIObjectTemplate {
public Texture2D texture;
public Rect area;
public int test = 1;
public GUIObjectTemplate() {
//Constructor
}
// Use this for initialization
public virtual void Start () {
}
// Update is called once per frame
public virtual void Update () {
}
}
The class isn't used at all by itself (maybe I should make it an abstract class). "texture" and "texture_alt" are assigned in the subclass via inspector. "area" is assigned in the constructor of the subclass.
To give some perspective on what I'm trying to do, I'm drawing elements (tabs in this case) for a GUI. The specific problem I'm having is in the Draw() method of the subclass. The GUI.DrawTexture() calls crash the program with a NullReferenceException. I think this is caused by the derived class not holding it's instance variables properly.
thanks for the help so far : 3
Subclass:
using UnityEngine; using System.Collections;
public class GUITab : GUIObjectTemplate {
public Texture2D texture_alt; public bool selected = false; public int test1;
// Use this for initialization
public override void Start () {
//Debug.Log(test1);
}
// Update is called once per frame
void Update () {
}
//Constructor
public GUITab(Rect _area) {
area = _area;
}
public void Draw() {
if (MouseOver()) {
if (MouseDown()) {selected = true;}
}
if (selected || MouseOver()) {
GUI.DrawTexture(area, texture);
}
else {
GUI.DrawTexture(area, texture_alt);
}
}
/*These methods are used for simplifying talking to the mouse*/
bool MouseOver() {
if (area.Contains(new Vector2(Input.mousePosition.x, Screen.height - Input.mousePosition.y))) {
return true;
}
return false;
}
bool MouseDown() {
if (Input.GetMouseButtonDown(0)) {return true;} return false;
}
}
Answer by Waz · Jul 30, 2011 at 12:01 AM
You've got the same variable in both superclass and subclass. You don't want to do that. Certainly Unity will only present one of them.