|
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; } 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; }
(comments are locked)
|
|
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.
(comments are locked)
|
|
Superclass:
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:
(comments are locked)
|
|
also mouse should be Input.GetMouseButtonDown(0) or Input.GetMouseButtonup(0)
(comments are locked)
|
|
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 That should also work for any inheritance scheme as expected. Here is the full code: 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 Remember also that your sub class
Jul 29 '11 at 10:09 PM
Herman Tulleken
(comments are locked)
|

Can you please format your code properly?