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 TrivialRoo · Jun 11, 2014 at 08:13 PM · collidernullreferenceexception

NullReferenceException by trying to access another script.

Yes, I know NullReferenceException is a very common error and in most cases it is the same, but in this case I have not found anything about it on Google or Unity. Here is my first script:

 void Update() {
 
         ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 
         if (isSelected) {
             if (currentBuilding != null && !hasPlaced) {
 
                 obj_Check scriptDisable = currentBuilding.GetComponent<obj_Check>();
 
                 if(Physics.Raycast(ray, out hit, Mathf.Infinity)) {
                     currentBuilding.position = new Vector3(hit.point.x, 0, hit.point.z);
 
                     if(Input.GetMouseButtonDown(0)) {
                         if (IsLegalPosition()) {
                             hasPlaced = true;
                             isSelected = false;
                         }
                     }
                 }
             }
         }
         else {
             if(Input.GetMouseButtonDown(0)) {
                 if (Physics.Raycast (ray, out hit, Mathf.Infinity)) {
                     if(hit.transform.gameObject.tag == "Placeable") {
                         pl = hit.transform.gameObject;
                         selectObj = pl.GetComponent<SelectionScript>();
                         selectObj.selected = true;
                     }
                     else {
                         if(pl != null) {
                             selectObj = pl.GetComponent<SelectionScript>();
                             selectObj.selected = false;
                             pl = null;
                         }
                     }
                 }
             }
         }
     }
 
     bool IsLegalPosition() {
         if(checker.colliders.Count > 0) {
             return false;
         }
         return true;
     }

And here is my second script, obj_Check in which the first script references:

     public List<Collider> colliders = new List<Collider>();
 
     void OnTriggerEnter(Collider c) {
         if (c.tag == "Placeable") {
             colliders.Add (c);
         }
     }
 
     void OnTriggerExit (Collider c) {
         if (c.tag == "Placeable") {
             colliders.Remove (c);
         }
     }

Now whenever I select my building or whatever, and try to place it down on the ground, it gives me this error: NullReferenceException: Object reference not set to an instance of an object gameController.IsLegalPosition () (at Assets/Scripts/gameController.cs:128) gameController.Update () (at Assets/Scripts/gameController.cs:99)

I have no idea why... Thanks in advance for your help!

Comment
Add comment · Show 4
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 roojerry · Jun 11, 2014 at 08:16 PM 0
Share

Where does "checker" get set? That is most likely what is set to null. If it is supposed to be set on the script in the inspector, double check it.

avatar image Jeff-Kesselman · Jun 11, 2014 at 08:16 PM 0
Share

We need to see the whole gameController.cs script. Otherwise we have no idea where those lines are.

avatar image NoseKills · Jun 11, 2014 at 08:22 PM 1
Share

The error is clearly(?) pointing to the IsLegalPosition() method, and there's only 2 things there that possibly could be null:

 checker

or

 checker.colliders

Your script doesn't show how you initialize the check variable and whether it's set to null at some point.

Unless it's a very special case or a known bug in a code library that is being used, Null Reference Errors can seldom be solved by Googling other peoples problems. It's like Googling "what is this loud noise in my backyard" :) It easier to look yourself once you know how to.

One way to get started is to just have to look at the line the error is pointing to (double click the error in Unity), and for example Debug.Log() the values of all the variables on that line to pin down the variable that is null. Then start tracking down why... :)

avatar image TrivialRoo · Jun 11, 2014 at 08:30 PM 0
Share

O$$anonymous$$. I figured it out. The cube was in an empty gameobject. The GA$$anonymous$$EOBJECT parent was the one that got instantiated.. I'm sorry xD

0 Replies

· Add your reply
  • Sort: 

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

24 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

Related Questions

Getting NullReferenceException from the enemies my spawner makes and it won't collide with my weapon. 0 Answers

NullReferenceException when trying to get Collider2D's parent gameObject 1 Answer

ControllerColliderHit Vairable object Instance Exception in c#; 2 Answers

null new collider for raycast grabbing 1 Answer

Raycast causes game to crash when shooting up 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