x


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;
}

}

more ▼

asked Jul 29 '11 at 04:55 PM

crump3ts gravatar image

crump3ts
1 5 5 6

Can you please format your code properly?

Jul 29 '11 at 05:19 PM Herman Tulleken
(comments are locked)
10|3000 characters needed characters left

4 answers: sort newest

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.

more ▼

answered Jul 30 '11 at 12:01 AM

Waz gravatar image

Waz
6.5k 22 33 71

(comments are locked)
10|3000 characters needed characters left

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;
    }

}
more ▼

answered Jul 29 '11 at 06:05 PM

crump3ts gravatar image

crump3ts
1 5 5 6

(comments are locked)
10|3000 characters needed characters left

also mouse should be Input.GetMouseButtonDown(0) or Input.GetMouseButtonup(0)

more ▼

answered Jul 29 '11 at 05:28 PM

conflictbliz gravatar image

conflictbliz
26 14 16 19

(comments are locked)
10|3000 characters needed characters left

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);
    }
}
more ▼

answered Jul 29 '11 at 05:25 PM

Herman Tulleken gravatar image

Herman Tulleken
1.6k 25 36 56

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.

Jul 29 '11 at 05:33 PM crump3ts

Show us the superclass. And format the code in the question (highlight and press the 101010 button)

Jul 29 '11 at 05:43 PM Waz

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().

Jul 29 '11 at 10:09 PM Herman Tulleken
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x344
x337
x204
x135
x38

asked: Jul 29 '11 at 04:55 PM

Seen: 1055 times

Last Updated: Jul 30 '11 at 12:01 AM