• 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
Question by rdanielsson · May 27, 2012 at 08:01 PM · null

Object reference not set to an instance of an object: Can't seem to work things out

I can't seem to figure out what I am doing wrong with this script. I can launch my game but whenever i am colliding with a pickup Unity closes the game and tells me there is a NullReferenceExeption going on. Been looking around for some time but can't seem to find an answer.

Here is the script ( Javascript ):

     // This script is supposed to find the GUI texture in my GUIpoint GameObject?
 // ( I think GUItextures are gameobjects? )
 
 
 
 static public var onPoint : boolean = false;
 
 static var DetectPoint : GameObject;
 
 DetectPoint = GameObject.Find("GUIpoint");
 
 
 static function hideTexture(){
 
 if(onPoint == true){
 
 
 DetectPoint.guiTexture.color.a = 255;
 
 yield WaitForSeconds (1);
 
 DetectPoint.guiTexture.color.a = 0;
 
 onPoint = false;
     
     }    
 
 }

EDIT:

The error is in the script that calls the coroutine:

 GetComponent(PointGet).StartCoroutine("hideTexure");

Many thanks in advance :)

Comment

People who like this

0 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 kolban · May 27, 2012 at 08:32 PM 0
Share

When Unity detects an error, it usually says which line the error occurs at. Can you indicate in your post which line the NullReferenceExpcetion is being reported?

2 Replies

· Add your reply
  • Sort: 
avatar image

Answer by kolban · May 27, 2012 at 08:56 PM

If the error is being reported in the line which reads:

 GetComponent(PointGet).StartCoroutine("hideTexture");

This tells me that either:

  1. There is no such data type (Script) called "PointGet")

  2. The object in which GetComponent() is being called doesn't have a component of type PointGet attached to it and hence returns "null". Since it returns null, the code is equivalent to:


    null.startCoroutine("hideTexture");


Which would generate the runtime error you describe.

Comment

People who like this

0 Show 1 · 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 rdanielsson · May 27, 2012 at 09:03 PM 0
Share

The script where the code is on is called "PointGet" and is attached to my gameobject which is a GUI texture. That is mostly why I simply can't find a reason for this null that is happening.

avatar image

Answer by rdanielsson · May 27, 2012 at 11:23 PM

Solved this error by replacing this: ( EDIT: It didn't solve anything )

 GetComponent(PointGet).StartCoroutine("hideTexure");

With this:

 GetComponent(PointGet);
 
 PointGet.hideTexture();

2nd Edit:

I have replaced GetComponent(PointGet);

With:

 var PointGetScript: PointGet = GetComponent(PointGet); 

No errors, but it doesn't start my hideTexture function as intended, Oh and it does display the guitexture when i delete the yield WaitForSeconds (1);in my PointGet script. I think I am triggering the coroutine wrong.

Edit #3:

The script where i am triggering the coroutine from looks like this:

 if(shipTrigCollision.gameObject.tag == "TrianglePickup"){
 
  
 PointGet.onPoint = true; // Sets the statement as true in our pointget script
 
 var PointGetScript: PointGet = GetComponent(PointGet);
 
 PointGetScript.hideTexture();
 
 
 audio.PlayOneShot(trianglePickupCollideSound);
     
      Destroy(shipTrigCollision.gameObject); // Destroys the game object we collided with last
      
      }


And the script where the coroutine is on looks like this:

 static public var onPoint : boolean = false;
 
 static var DetectPoint : GameObject;
 
 DetectPoint = GameObject.Find("GUIpoint");
 
 DetectPoint.guiTexture.enabled = false; // Disables guiTexture
 
 
 static public function hideTexture(){
 
 
 
 
 if(onPoint == true){
 
 
     DetectPoint.guiTexture.enabled = true;
 
     yield WaitForSeconds (1);
 
     DetectPoint.guiTexture.enabled = false;
     onPoint = false;
     
     }    
 
 }



Still very confused where the error is.

Comment

People who like this

0 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 whydoidoit · May 27, 2012 at 11:26 PM 0
Share

That line GetComponent(PointGet) is doing nothing at all. You already have a variable called PointGet, but GetComponent is called for a class not a variable - it's confusing because I bet your script is also called PointGet right?

avatar image rdanielsson · May 28, 2012 at 08:54 AM 0
Share

Yeah, I noticed it didn't do very much :p Yes, my script is called PointGet, Isn't it that script I am refering to?

avatar image whydoidoit · May 28, 2012 at 10:07 AM 0
Share

I think we need to see a bit more code to give it some context :) Can you edit the answer above or the question and post the code for both of the routines. You know how it is, it'll be something you missed in a bit you think is dead right, or something (that's what always happens to me, anyway!)

avatar image rdanielsson · May 28, 2012 at 10:23 AM 0
Share

Okay, i added both parts :)

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Array element coming as not null 2 Answers

Object keeps moving up even when StopCoroutine is called 2 Answers

checking for null not working 1 Answer

Access components in children of a prefab? 5 Answers

Null values in Unity 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