• 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
1
Question by Reafy · Oct 11, 2014 at 10:46 AM · nullreferenceexception

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

this is the error i get when i try to run my code. I'm trying to make a whole bunch of fences disappear. All the fences are parented to one empty child object, so i don't have to individually remove each one of them.

I'm calling the function to remove the fences here:

 function canPurchase ()
 {
     if (totalScore >= 50)
     {
         totalScore -= 50;
         GameObject.Find("pitVendorRaycast").GetComponent(pitVendorRay).purchase();
         GameObject.Find("pitBoudary").GetComponent(pitPurchased).purchased(); //code to call function
     }
 }

and actually running the function here:

 #pragma strict
 
 function purchased ()
 {
     renderer.enabled = false;
 }
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
2
Best Answer

Answer by Raiden-Freeman · Oct 11, 2014 at 11:55 AM

GameObject.Find() if it doesn't find anything, it will return null. So you're trying to call null.GetComponent() which will not happen. Also even if Find() finds something every time, maybe this something, lacks the component pitVendorRay. You should do the following:

 var foundGameObject = GameObject.Find("pitVendorRaycast");
 if (foundGameObject!=null)
 {
      var rayComponent = foundGameObject.GetComponent(pitVendorRay);
      if (rayComponent!=null)
           rayComponent.purchase();
      else
           Debug.Log("The object "  + foundGameObject + " doesn't have a pitVendorRay Component");
 }
 else
      Debug.Log("Could not find an object with name pitVendorRaycast");

The reason it returns null, is maybe because you have a different name in the object, or because you forgot to add a component. To make sure that you have a component, you can write the following attribute in C#:

 [RequireComponent(typeof(pitVendorRaycast))]

write this on a C# script and attach this script to the object. If it lacks the component, unity will create a componenet of pitVendorRaycast and add it to the same object that has this attribute. more on attributes here and better here.

On a side note, GameObject.Find() is kinda slow and you should avoid it. Instead, you have multiple options: 1)if it's just one object that you need to find, and it always exists, make a variable like that: GameObject objectToFind; and in the editor, you will see this variable on the inspector. You can drag n drop from the hierarchy the object that you wanted to find. 2)if it's multiple objects, you can use a collection of them to store them. For example, let's say that you want to stop all bullets in the scene. Every time that you create a bullet, you should add it to a collection:

 var newBullet = Object.Instantiate(Bullet); //you create a new bullet
 //have a collection, for example a list, by including System.Collections
 //public List<Bullet> bulletList;
 //this should be in a class somewhere
 //bulletList = new List<Bullet>();
 //write the above in a start function of the same class
 //then every time you create a new bullet
 bulletList.Add(newBullet);

and afterwards you can go through each bullet, without having to find them with foreach:

 foreach(Bullet bullet in bulletList)
 {
     bullet.FindAComponenetOrSomething();
 }

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 dmg0600 · Oct 11, 2014 at 12:07 PM 2
Share

GameObject.Find(string name) look for the GameObject name, not for the tag or its components. $$anonymous$$ake sure you have named the GameObjects pitVendorRaycast and pitBoudary, case included.

avatar image Reafy · Oct 11, 2014 at 12:17 PM 0
Share

god damn it was just a typo. I really appreciate your help and the effort you put into your answer though :)

avatar image Raiden-Freeman · Oct 12, 2014 at 07:46 AM 0
Share

Yes sorry I never use .Find, always FindGameObjectsWithTag because it's faster.

In order to avoid such mistakes (which happen to the best), you shouldn't try to find objects like that. Either add them to a collection when you're creating them, or use the inspector.

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

29 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

Related Questions

Help with NullReferenceException 2 Answers

Beginner having trouble with NullReferenceException 2 Answers

NullReferenceException when sending message to another object...really stumped. 1 Answer

Problem with Null Reference Exeption??:/ 1 Answer

NullReferenceException: Object reference not set to an instance of an object 0 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