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 Sawula · May 31, 2015 at 08:05 PM · errorgameobjectcolliderreference

Reference to Object error but script works fine

Hello to all I got this script (below) Which I use to get a variable from another gameobject stocked onto this one. It check for what item is colliding with this one, and then get the script Cell3 from the colliding item, and stock the gridX variable from it. It's an instantiated object so I can't use ColliderStay, it simply doesn't work, so I used OverlapSphere as a workaround.

The problem is that I get an error message with this when I play, even though the game play perfectly fine. "NullReferenceException: Object reference not set to an instance of an object virustir.Update () (at Assets/scripts/virustir.cs:26)"

I thought at first it was because the collider was detected wrong but after many check, I realize the item also detect itself and there is no Cell3 on itself. Is there a way to tell it that if there is no Cell3 script, then to just ignore the item? Or maybe to not have it check itself? (I tried to use layers, but to no avail).

I really want to understand what is that error, and how to remove it, I'm worried it might break my code later in my project...

Thanks in advance!

 using UnityEngine;
 using System.Collections;
 
 public class virustir : MonoBehaviour {
 
     public Cell3 gridX;
     public int gix;
     public Collider[] colliding;
 
     
     // Update is called once per frame
     void Update () {
   
     
         colliding = Physics.OverlapSphere(transform.position, this.GetComponent<SphereCollider>().radius);
         if(colliding != null && colliding.Length > 0){
             for (int i = 0; i < colliding.Length; i++) {
                         Cell3 c = colliding[i].GetComponent<Cell3> ();
                         gix = c.gridX; //line with the error on it
                 }
         }
     }
 }
Comment
Add comment · Show 6
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 pako · Jun 01, 2015 at 11:10 AM 0
Share

It is not quite clear what you want to do, but this is what I make of the the code I read:

You define gridX as an instance variable of the virustir class, which is of type Cell3. Then in the line with the error you are trying to assign the gridX instance variable, which belongs to the 'c' instance, which is also of type Cell3. I don't know the type of this c.gridX, but you are assigning it to gix, which is an int. So, if c.gridX is not an int, it would not work, even if you didn't get the error you're getting.

The NullReference error is most probably because c.gridX is not an int but of type Cell3, and nothing has yet been assigned to it. In other words you're trying to access the c.grid object, but it in null, so there's nothing to access.

avatar image Saad_Khan · Jun 01, 2015 at 11:36 AM 3
Share

You can put a null check to get rid of the error... try

if(c!=null) gix = c.gridX

avatar image Bonfire-Boy · Jun 01, 2015 at 03:19 PM 0
Share

@pako But a) c.gridX is a different gridX to the one shown in the code, presumably there's a gridX in Cell3 which is an int (otherwise this wouldn't compile).

$$anonymous$$ore importantly though, b) there's nothing to check that the members of colliding all have Cell3 components. colliding could be non-null and have oodles of members without c ever being set to something that's non-null (ie when none of them have Cell3 components). So the null check would help to protect against NREs as well as helping to track down what's going wrong.

@Sawula The effect of the NRE is just that gix doesn't get set for that object, or for any subsequent objects in the colliding array. So gix stays at whatever value it was before. That'll be why things appear to run ok despite the NR$$anonymous$$ So add the null check, and put some logging in when c is null so you can see which if any of the colliders returned by OverlapSphere don't have Cells3 components.

Also, back to my first point, you'll find it easier to debug things (and it'll be easier for people to help you) if you get into the habit of not using the same variable name for different types of things in different classes.

avatar image Bonfire-Boy · Jun 01, 2015 at 04:23 PM 0
Share

@pako Sorry but you're misunderstanding what I said, your last comment contains virtually nothing which is correct.

It's not that c.gridX belongs to a different instance, it's that c is an instance of a different class ( Cell3, the code for which we have not been shown and in which gridX must be a int or something that can be implicitly cast to one in order for the above code to compile).

Also, the compiler does not pick up on NREs. NREs happen at runtime. The fact that they're getting NREs from the above code means that it compiled and so your int/Cell3 analysis is not correct.

And the c variable has been set using GetComponent(), so checking c against null is exactly what they need to do to check if the object has a Cell3 component.

avatar image pako · Jun 01, 2015 at 04:51 PM 0
Share

"It's not that c.gridX belongs to a different instance, it's that c is an instance of a different class" Actually, that's what I meant, I just didn't phrase it properly.

"Also, the compiler does not pick up on NREs. NREs happen at runtime". Glad to know, thanks! I just fix them, when I get them.

"And the c variable has been set using GetComponent(), so checking c against null is exactly what they need to do to check if the object has a Cell3 component." You are absolutely right! Thanks for correcting me, I don't know what I was thinking... $$anonymous$$ind block maybe :-(

Show more comments

1 Reply

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

Answer by Bonfire-Boy · Jun 01, 2015 at 05:16 PM

 Cell3 c = colliding[i].GetComponent<Cell3> ();
 gix = c.gridX; //line with the error on it

The only way that line can throw a NRE is if the c variable is null, and it will be null only if the object that colliding[i] is on has no Cell3 component (because GetComponent<> returns null if it can't find a component of the required type) .

The easy way to avoid the NRE is, as Saad_Khan said in a comment, to check c against null. If you want to know more about what's going on, add some logging when it fails. Something like...

 if (c != null)
 {
     gix = c.gridX;
 }
 else
 {
     Debug.LogWarning("Collided with "+colliding[i].gameObject.name+", which has no Cell3 component");
 }

Comment
Add comment · 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

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

23 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

Related Questions

OnCollisionEnter2D not working? 2 Answers

[UNSOLVED] Object reference not set to an instance of an object 0 Answers

Why am I getting this error ? (Animation based) 3 Answers

"Object reference not set to an instance of an object" problem 1 Answer

Reference GameObject from Bounds.Contain 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