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 mediamavrick · Mar 05, 2014 at 04:57 AM · nullreferenceexceptionjava

Object reference not set to an instance of an object issue

For some odd reason, my If statement in the Paddle class keeps giving me an error. The error says "NullReferenceException: Object reference not set to an instance of an object". I cant wrap my head around this problem. I need to get the Boolean value of "isBlue" from the Ball Class into the Paddle class. Once it is in the Paddle class, I need to use that boolean value to transform a texture. Any Help would be greatly Appreciated. Thanks

  //  

Paddle class

 #pragma strict
 
 var blue: Texture;

 
 var isBlue: boolean = false; 
 Public var newBall : Ball;

   
 function Start () {
 
 }
   
 function Update () {
 
 newBall = GetComponent(Ball);
 isBlue = newBall.isBlue;
 
 if(isBlue == true){
 
 renderer.material.mainTexture = blue;
 }

 }

Ball Class

 var blue : Texture;
 
  var isBlue : boolean = false;
 
  
 
 function OnCollisionEnter(col : Collision){
 
 if(col.collider.name == "Brick3"){
 Destroy(col.gameObject);
 renderer.material.mainTexture = blue;
 isBlue = true;
 
 }
 }
Comment
Add comment · Show 2
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 robertbu · Mar 05, 2014 at 04:58 AM 0
Share

What line is giving you the null reference exception?

avatar image mediamavrick · Mar 05, 2014 at 05:10 AM 0
Share

Line 15: isBlue = newball.isBlue; Any sugestions?

1 Reply

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

Answer by robertbu · Mar 05, 2014 at 05:55 AM

This line will fail if the GetComponent(Ball) call fails to find the 'Ball' component. Note this call is expecting that the 'Ball' script will be on the same game object as the script above. If it is on another game object (like I would expect since this is the Paddle script), then you need to first get access to that other game object and then use that game object in the GetComponent() call:

  newBall = GameObject.Find("Ball").GetComponent(Ball);

Where the ball object has the name 'Ball'. Note it would be more efficient to put the GetComponent() call in Start() and only call it once if there is only one ball during the life of the game.

Comment
Add comment · Show 6 · 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 mediamavrick · Mar 05, 2014 at 06:29 AM 0
Share

Thank you very $$anonymous$$uch Sir. You have no idea how much I appreciate this answer. It solved my problem I have been trying to figure out for almost 24 hours. I was starting to give up. Thank you so much/

avatar image Kiloblargh · Mar 05, 2014 at 08:03 AM 0
Share

it would be more efficient to put the GetComponent() call in Start() and only call it once

Second that! Don't GetComponent or Find in Update; it is never necessary to do that and will slow your game down.

Also, much less important tip: you never need to say if (someBool == true), because it is equivalent to say " if (someBool).

Less obviously, you can check if (newBall). If there is no newBall, it will return false, and you can avoid doing whatever was going to cause the error until there is one.

avatar image mediamavrick · Mar 05, 2014 at 10:27 AM 0
Share

I'm a little confused as to what @$$anonymous$$iloblargh is saying about the boolean. how is if(someBool==true) equal to if(someBool). if someBool is a variable it can be either true or false. and depending on its value it could change the output of the program. $$anonymous$$aybe Im missunderstanding what you talking about. Could you futher explaine. And I have another question for both of you. @robertbu I put the "newBall = GameObject.Find("Ball").GetComponent(Ball);" statment in the start method and just put my if statements in the update method. Will that also slowdown my game?

avatar image mediamavrick · Mar 05, 2014 at 10:34 AM 0
Share

I got rid of if(isBlue == true) and replaced it with if(isBlue) and the program ran just fine. Im still confused as to how they are the exact same statement though. cause what if isBlue is set to false. lol

avatar image robertbu · Mar 05, 2014 at 02:42 PM 0
Share

GameObject.Find() is slow. Doing it once in Start(), or once in a while as the game is running is fine. But doing it in Update() is discouraged. Think about how many times things execute in Update. If your app is running at 60 fps, in five $$anonymous$$utes of game play the function would execute 18,000 times. With that said, chances are it would not slow down your game (based only on your class names). If your game is running at 60 fps you can do a certain amount of work during a single frame. So your game needs a certain amount of work/complexity before it cannot get all its work done in 1/60 of a second and therefore slows down. A typical pong-like game does not do much work in a frame, so adding a GameObject.Find() every frame would not slow it down. Note if you needed to get it every frame, finding your object by tag is efficient enough to use every frame. So you would tag your ball 'Ball' and then you could use GameObject.FindWithTag().

As for the 'if' statement, the 'if' will execute if whatever is inside it is true. So a way to think about it is to replace 'isBlue' with its value. So if 'isBlue' is 'true', then it is equivalent to 'if (true)' which will execute. Once you get your brain wrapped around that one, then figure out the opposite. I see a lot of beginning programmers write something like 'if (isBlue == false)' that is equivalent to the the more professional 'if (!isBlue)'.

Show more comments

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

20 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

Related Questions

Getting java.lang.Exception error on android and game ends up crashing. 0 Answers

NullReferenceException After A Few Hours? 1 Answer

Object reference not set to an instace of an object (JS) 0 Answers

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

NullReferenceException 3 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