Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 AusieJamster · Apr 09, 2015 at 10:04 AM · nullreferenceexceptionpublic variable2d array

NullReferenceException on Public Variable only Outside the Class

It doesn't really make sense to me that inside the class I have set up the variable called allTiles and gave it multiple values but every time i refer to allTiles outside the class i get a NullReference Error as if all the values are there but are set to null D:

The TileHill is a sub-class of Tile and they both have constructors to set numUnits to 0.

 public class TileManager : MonoBehaviour {
     
     public const int TILES_X = 12;
     public const int TILES_Y = 6;
     
     public Texture[] terran = new Texture[2];
 
     public Tile[,] allTiles = new Tile[TILES_X, TILES_Y];
 
     void Start () {
         for(int i = 0; i < TILES_Y; i++) {
             for(int j = 0; j < TILES_X; j++) {
                 if(Random.Range(0.0f, 1.0f) < 0.5f){
                     allTiles[j, i] = new Tile();
                 }
                 else{
                     allTiles[j, i] = new TileHill();
                 }
             }
         }
     }
 }


 public class GameScreen : MonoBehaviour {
     
     TileManager tileManager = new TileManager();
     GameManager gameManager = new GameManager();
 
     void OnGUI () {
         int BTN_SIZE_X = Screen.width/12;
         int BTN_SIZE_Y = Screen.height/8;
 
         float groupSizeX = BTN_SIZE_X * TileManager.TILES_X;
         float groupSizeY = BTN_SIZE_Y * TileManager.TILES_Y;
 
         GUI.BeginGroup(new Rect((Screen.width - groupSizeX) / 2f, 0, groupSizeX, groupSizeY));
 
         for(int i = 0; i < TileManager.TILES_X; i++) {
             for(int j = 0; j < TileManager.TILES_Y; j++) {
                 if(GUI.Button(new Rect(BTN_SIZE_X*i, BTN_SIZE_Y*j, BTN_SIZE_X, BTN_SIZE_Y), "" + tileManager.allTiles[0,0].numUnits)){
                     gameManager.setSelection(i, j);
                 }
             }
         }
 
         GUI.EndGroup();
     }
 }
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 Mewada-Neil · Apr 09, 2015 at 11:24 AM 0
Share

First of all, unity editor will give you a warning that you cant instantiate a class which is child class of monobehaviour. You did that in the line:- Tile$$anonymous$$anager tile$$anonymous$$anager = new Tile$$anonymous$$anager();

You cant instantiate the whole monobehaviour again

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by troien · Apr 09, 2015 at 11:25 AM

First of all, sinse TileManager is a MonoBehaviour, you can't instantiate it using the 'new' keyword (Unity should display a warning in the console notifying you about this I believe)

That leaves me to what is most likely your problem, GameScreen.tileManager is not set. sinse this is a private variable and you can't instantiate it using the new keyword, so tileManager is null ;) Simplest thing would be to make it a public variable and assign it in the inspector. Or use another way to find the TileManager and assign it in the Awake/Start (GameObject.Find/GetComponent/etc.)

Same thing counts for the GameScreen.gameManager if this is a MonoBehaviour btw.

Comment
Add comment · Show 4 · 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 AusieJamster · Apr 10, 2015 at 12:38 AM 0
Share

Hmm so ins$$anonymous$$d of Tile$$anonymous$$anager tile$$anonymous$$anager = new Tile$$anonymous$$anager(); How would I call it ? Tile$$anonymous$$anager tile$$anonymous$$anager = Tile$$anonymous$$anager();?? or something like that??

avatar image Mewada-Neil · Apr 10, 2015 at 04:00 AM 0
Share

No, not like that. Create two different scripts for both classes and attach both of them on same gameobject get reference using GetComponent method.

avatar image Mewada-Neil · Apr 10, 2015 at 04:01 AM 0
Share

No, not like that. Create two different scripts for both classes and attach both of them on same gameobject get reference using GetComponent method.

avatar image troien · Apr 10, 2015 at 07:58 AM 0
Share

Well, how you would call it depends on how you intend to use it. Sinse there are many ways.

One of them would be to make Tile$$anonymous$$anager a class that does not extend $$anonymous$$onoBehaviour (Note that when you do that, Start,Awake and Update won't work as they are callbacks that are called by $$anonymous$$onoBehaviour. It then is a plain old C# class, which would mean you have to replace Start with a constructor). Andvantage of this is that you can call that constructor using the 'new' keyword like you do now. See also this question.

If you want it to be a $$anonymous$$onoBehaviour, here are a few options:

 public class GameScreen : $$anonymous$$onoBehaviour
 {
     // This way you should set them in the inspector of GameScreen
     public Tile$$anonymous$$anager tile$$anonymous$$anager;
     public Game$$anonymous$$anager game$$anonymous$$anager;
 }
 
 public class GameScreen : $$anonymous$$onoBehaviour
 {
     // If you dont want to set it in the inspector, and simply want to create a new Tile$$anonymous$$anager and Game$$anonymous$$anager for every GameScreen you have
     Tile$$anonymous$$anager tile$$anonymous$$anager;
     Game$$anonymous$$anager game$$anonymous$$anager;
 
     private void Awake()
     {
         tile$$anonymous$$anager = gameObject.AddComponent<Tile$$anonymous$$anager>();
         game$$anonymous$$anager = gameObject.AddComponent<Game$$anonymous$$anager>();
     }
 }
 
 [RequireComponent(typeof(Tile$$anonymous$$anager), typeof(Game$$anonymous$$anager))]
 public class GameScreen : $$anonymous$$onoBehaviour
 {
     // This one is the same as the previous, but the difference is that when you now add a GameScreen to anything in the scene, Tile$$anonymous$$anager and Game$$anonymous$$anager will automatically be added
     Tile$$anonymous$$anager tile$$anonymous$$anager;
     Game$$anonymous$$anager game$$anonymous$$anager;
 
     private void Awake()
     {
         tile$$anonymous$$anager = GetComponent<Tile$$anonymous$$anager>();
         game$$anonymous$$anager = GetComponent<Game$$anonymous$$anager>();
     }
 }


For the last example, see also: RequireComponent

avatar image
0

Answer by Mewada-Neil · Apr 09, 2015 at 11:40 AM

As I mentioned in the comment, Unity prevents the GameScreen class from creating an instance of the TileManager class.

It would also happen if your GameManager class is also derived from MonoBehaviour class.

Thus the tileManager references will remain null.

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.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

[Solved] Actually totally explainable NRE with 2D array 0 Answers

Null exception for public variable 2 Answers

How to add a gameobject to a null gameobject 1 Answer

Why am I getting a NullReferenceException while trying to set a gameobjects animation frame? 0 Answers

NullReferenceException .. problem with Camera.main.transform 2 Answers


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