• 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 NorthernEagle · Apr 01, 2015 at 04:16 AM · c#access

Access subclass variables not allowed?

Hello, I have 2 scripts as follows:

 // Script_A.cs
 
 using UnityEngine;
 using System.Collections;
 
 public class Script_A : MonoBehaviour
 {
     public float number_One = 1;
 
     public class Script_A_Subclass : MonoBehaviour
     {
         public float number_Two = 2;
     }    
 }

 // Script_B.cs
 
 using UnityEngine;
 using System.Collections;
 
 public class Script_B : MonoBehaviour
 {
     public Script_A firstScript;
     public Script_A.Script_A_Subclass firstScriptSubclass;
 
     void Update ()
     {
         firstScript.number_One = 100001;
         firstScriptSubclass.number_Two = 22222;
     }
 }

The issue I'm having is I can access from Script_B.cs the variable number_One but not number_Two. Does Unity3D allow access to subclass variables like this? If so, how do you access them? I'm getting an error that "NullReferenceException: Object reference not set to an instance of an object Script_B.Update () (at Assets/Script_B.cs:14)".

Thanks again.

Comment
Add comment · Show 3
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 Addyarb · Apr 01, 2015 at 05:22 AM 1
Share

Are you setting their value in the start function with GetComponent?

avatar image NorthernEagle · Apr 01, 2015 at 05:32 AM 0
Share

So in Script_B.cs, I should have a Start function that sets the links to Script_A.cs?

avatar image Addyarb · Apr 01, 2015 at 05:37 AM 1
Share

Unless you are just dragging the scripts in via the inspector. But yes, try GameObject.Find ("GameObjectNameHere").GetComponent ();in the Start function.

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by NorthernEagle · Apr 02, 2015 at 07:53 PM

Ok so I've been bashing my head here trying to figure this coding out. Thank you all for the help. I've added the following to Script_B.cs:

 void Start ()
     {
         // add components here
         firstScript = cube.AddComponent<Script_A> () as Script_A;
     }

That code loads the script perfectly onto my test cube gameObject but I can only access the parent class in Script_A.cs. All the Script_A.cs child class are untouchable and blind to Script_B.cs.

Comment
Add comment · Show 2 · 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 Bonfire-Boy · Apr 02, 2015 at 08:33 PM 1
Share

What do you mean by "All the Script_A.cs child class are untouchable and blind to Script_B.cs."? The subclass is public in Script_A so why is it any less accessible to Script_B than the base class is?

avatar image NorthernEagle · Apr 02, 2015 at 10:25 PM 0
Share

Wish I knew why I can't access the children in Script_A from Script_B. I've got it to work well now with the following code: The addition of Static and direct calling from Script_B works now.

 // Script_A.cs
 
 using UnityEngine;
 using System.Collections;
 
 public class Script_A : $$anonymous$$onoBehaviour
 {
     public static float number_One = 1;
 
     public class Script_A_Subclass : $$anonymous$$onoBehaviour
     {
         public static float number_Two = 2;
     }    
 }
 
 // Script_B.cs
 
 using UnityEngine;
 using System.Collections;
 
 public class Script_B : $$anonymous$$onoBehaviour
 {
     public Script_A firstScript;
     public Script_A.Script_A_Subclass firstScriptSubClass;
     public GameObject cube;
 
     void Start ()
     {
         // add components here
         //firstScriptSubClass = cube.AddComponent<Script_A.Script_A_Subclass> () as Script_A.Script_A_Subclass;
     }
 
     void Update ()
     {
         Script_A.number_One = 11111111;
         Script_A.Script_A_Subclass.number_Two = 22222222;
 
         // print out values
         Debug.Log ("NumberOne: " + Script_A.number_One + "NumberTwo: " + Script_A.Script_A_Subclass.number_Two);
     }
 }
avatar image
2

Answer by sumeeton · Apr 01, 2015 at 06:01 AM

You haven't initialized the variables, hence the NullReferenceException

 // Script_A.cs
  
  using UnityEngine;
  using System.Collections;
  
  public class Script_A : MonoBehaviour
  {
      public float number_One = 1;
  
      public class Script_A_Subclass : MonoBehaviour
      {
          public float number_Two = 2;
      }    
  }
  
  // Script_B.cs
  
  using UnityEngine;
  using System.Collections;
  
  public class Script_B : MonoBehaviour
  {
      public Script_A firstScript;
      public Script_A.Script_A_Subclass firstScriptSubclass;
  
      void Start ()
      {
           // Initialize here --------------------------------------
           firstScript = new Script_A();
           firstScriptSubclass = Script_A.Script_A_Subclass();
      }
      void Update ()
      {
          firstScript.number_One = 100001;
          firstScriptSubclass.number_Two = 22222;
      }
  }
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 trololo · Apr 01, 2015 at 07:31 AM 1
Share

Yes but no, you should not instantiate a $$anonymous$$onoBehaviour class with the new keyword. Use AddComponent ins$$anonymous$$d or search for the script in the scene. Op: You can also use static variables in your case.

avatar image sumeeton · Apr 01, 2015 at 07:57 AM 0
Share

At the first place, what's the use of subclass extending monobehavior? From what I see, he is just using Script_A as struct.

avatar image Bonfire-Boy · Apr 02, 2015 at 08:35 PM 1
Share

I was assu$$anonymous$$g it's just a simple example of an idiom they're working with. But I'm also struggling to see the point, when the subclass is public. Would be interested to know if there is one, it's something I'd not considered before.

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

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Accessing Variable from another Script.. with a twist 2 Answers

Making a bubble level (not a game but work tool) 1 Answer

Can't access a javascript static variable from c# script 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