• 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 Zacharias3690 · Oct 06, 2012 at 07:01 PM · javascriptnullreferenceexception

Getting "Object reference not set to an instance of an object" Error

         var ballArray : Array;
         ballArray = GameObject.FindGameObjectsWithTag("PunishmentBall");
         ballArray.Add(GameObject.FindGameObjectsWithTag("StandardBall"));
         ballArray.Add(GameObject.FindGameObjectsWithTag("PowerUpBall"));
 
         var balls : GameObject[] = new GameObject[ballArray.length];
 
         for(var i = 0; i < ballArray.length; i++)
         {
             (ballArray[i] as GameObject).GetComponent(BallController).startSlow = Time.time;
             (ballArray[i] as GameObject).GetComponent(BallController).isSlowed= true;
         }

I have a trigger object that reads what it collides with, then performs an action. The code is supposed to get all the balls on the screen, then slow them down, but I get

NullReferenceException: Object reference not set to an instance of an object

Whenever it collides with this ball. The error occurs at line 8 here,

(ballArray[i] as GameObject).GetComponent(BallController).startSlow = Time.time;

Comment
Add comment · Show 7
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 Eric5h5 · Oct 06, 2012 at 07:31 PM 0
Share

This is incorrect:

 var ballArray : Array;
 ballArray = GameObject.FindGameObjectsWithTag("PunishmentBall");

FindGameObjectsWithTag does not return Array, it returns GameObject[], and you really shouldn't use Array for anything else either (it's obsolete and you have to do casting when you want to retrieve a value).

 var ballArray = GameObject.FindGameObjectsWithTag("PunishmentBall");
avatar image Zacharias3690 · Oct 06, 2012 at 09:19 PM 0
Share

This creates a static array though. I need it dynamic because I'm pulling from 3 different tags to get all the balls on the scene

avatar image Eric5h5 · Oct 06, 2012 at 10:45 PM 0
Share

You can't get a dynamic array from FindGameObjectsWithTag. You don't need dynamic anyway, since you will know from the FindGameObjectsWIthTag calls how many there are in total.

avatar image Zacharias3690 · Oct 07, 2012 at 01:38 AM 0
Share

No it doesn't I need to get 3 tags and out them together to get a total

avatar image Eric5h5 · Oct 07, 2012 at 01:45 AM 0
Share

So just add them up.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by hvilela · Oct 06, 2012 at 07:31 PM

Do you have objects with "StandardBall" and "PowerUpBall" tags? Cause if you don't, it'll return null and add a null reference to your array. Be aware that TAG is not NAME. To find object by name you should use just "Find".

Also, are you sure that your objects have the BallController component?

Anyway, I recommend you to add a Debug to your code to check if all the entries in your array are valid:

 var ballArray : Array;
 ballArray = GameObject.FindGameObjectsWithTag("PunishmentBall");
 ballArray.Add(GameObject.FindGameObjectsWithTag("StandardBall"));
 ballArray.Add(GameObject.FindGameObjectsWithTag("PowerUpBall"));

 for(var i = 0; i < ballArray.length; i++)
 {
     if (ballArray[i] != null) {
         var ballController = (ballArray[i] as GameObject).GetComponent(BallController);

         if (ballController != null) {
             ballController.startSlow = Time.time;
             ballController.GetComponent(BallController).isSlowed= true;
         } else {
             Debug.Log(i + " has no ball controller");
         }
     } else {
         Debug.Log(i + " is null!");
     }
 }

PS: Your "balls" variable is doing nothing.

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 Zacharias3690 · Oct 06, 2012 at 09:24 PM 0
Share

Thanks, the balls variable was a different attempt, didn't realize I forgot to delete it. I put your code in, and I'm still getting the same error when you fetch the ballcontroller, but I check my prefabs, and all of the have the script attached.

avatar image hvilela · Oct 06, 2012 at 10:02 PM 0
Share

The objective of my code is not to solve the problem, but to identify it. Run it an look at the console output (Window -> Console).

avatar image Zacharias3690 · Oct 07, 2012 at 03:30 AM 0
Share

I did, that's what I meant, I get the error, no debug code

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

10 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

Related Questions

Null Reference Exception confusion 3 Answers

Split() into a fixed length array? 1 Answer

Why ExecuteInEditMode Always Causes NullReferenceException Errors Even In Clamping!! 2 Answers

NullReferenceException in follow function 0 Answers

How can I stop getting Null Reference Exception? 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