Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by molul · Mar 19, 2017 at 06:12 PM · script.instancenull reference exception

[SOLVED] NullReferenceException in a script that calls another object's script variable

Hi. I'm making a tetris-like game and I'm having a NullReferenceException I don't know how to solve. Here's the structure more or less:

-GameObject Spawner

-GameObject Piece

Spawner has a script that controls the game status, and everytime a piece touches ground, a matrix named "gameStatus" is updated with the piece's column, row and some variables.

I'd like to save the whole piece gameObject in the "gameStatus" matrix, so instead of saving all the piece's variables in the array, I can just get them from the "piece" field.

I have this struct:

 public struct gridGap {
     public bool empty;
     
     // these are my current variables, that I want to 
     // avoid saving and just save the piece
     public int id;
     public int hits;
     public int number;
     public string name;
     public float scale;
     public int col;
     public int row;
     
     public GameObject piece;
 }

Now, when a piece touches the ground, I can initialize the "piece" element like this:

     public void updateGameStatus ( float scale, int row, int col, int hits, int number, int id, GameObject piece, string name ) {
         if (row > -1) {
             gameStatus[row, col].name = name;
             gameStatus[row, col].number = number;
             gameStatus[row, col].hits = hits;
             gameStatus[row, col].empty = false;
             gameStatus[row, col].id = id;
             gameStatus[row, col].scale = scale;
             gameStatus[row, col].piece = piece;
         }

However, later in the code, if I want to print a piece's gameObject script variable like this:

                 debugMessage = gameStatus[i, j].number + ", " + 
                     gameStatus[i, j].piece + ", " + 
                     gameStatus[i, j].piece.GetComponent<PieceManager>().hits + ", " + // this line throws a NullReferenceError
                     Debug.Log(debugMessage);


I get a NullReferenceException ("NullReferenceException: Object reference not set to an instance of an object"). Even if I just try to print "gameStatus[i, j].piece.GetComponent()" I get that error.

I'm new to Unity and don't quite understand why I can access the "piece" gameObject but not its attached script to print some variables. Could you please let me know how to properly do this?

Thanks in advance :)

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by molul · Mar 19, 2017 at 06:45 PM

(I hate it when I find the solution ONLY shortly after I decide to ask for help...):

It was really easy: the whole gameStatus array's "piece" fields start as null objects. Thus, when I go through the whole array, when I try to print the script for a piece and that position is empty, I of course get that null alert.

Sorry!

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

96 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 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 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 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 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

Not sure what this error is telling/asking me to do? 1 Answer

#Need Fast Help# NullReferenceException: Object reference not set to an instance of an object 0 Answers

How do I fix a Null Reference Exception (object reference not set to an instance of an object)? 2 Answers

Object reference not set to instance for my timer and my text 0 Answers

2 same Scripts w/ GetComponent : one works, one doesn't 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