• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by crump3ts · Jul 29, 2011 at 04:55 PM · variablesclassinheritanceinstancefield

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 t$$anonymous$$s. 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 t$$anonymous$$s for initialization
 public override void Start () {        
     Debug.Log(test1); //should print 10, instead prints 0    : /
 }

}

T$$anonymous$$s 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 t$$anonymous$$s 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;
 }
 

}

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Herman-Tulleken · Jul 29, 2011 at 05:19 PM 0
Share

4 Replies

· Add your reply
  • Sort: 
avatar image
0

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 t$$anonymous$$ng 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 t$$anonymous$$s:

 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 somet$$anonymous$$ng that is easy to spot for testing
     } 
 }

 //-----
 
 class SubClass : SuperClass
 {
     //do not redefine test1 here!
     public void Start()
     {
         base.Start();
         Debug.Log(test1);
     }
 }

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image crump3ts · Jul 29, 2011 at 05:33 PM 0
Share
avatar image Waz · Jul 29, 2011 at 05:43 PM 0
Share
avatar image Herman-Tulleken · Jul 29, 2011 at 10:09 PM 0
Share
avatar image
0

Answer by conflictbliz · Jul 29, 2011 at 05:28 PM

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
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 t$$anonymous$$s 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 t$$anonymous$$s 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 t$$anonymous$$nk t$$anonymous$$s 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 t$$anonymous$$s 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;
 }

}

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Is Instance Of 3 Answers

C# Inheritance, base class attributes, override and null object 1 Answer

C# Inheritance - Reference the INSTANCE of a variable, is it possible? 1 Answer

efficient way to make RTS building instances 2 Answers

Can I/Should I call Awake() in parent class manually? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges