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 highpockets · Mar 05, 2013 at 02:02 AM · nullreferenceexceptionnull

Getting a strange NullReferenceException

I'm getting a NullReferenceException in this script

 public class ContainerBehaviour : MonoBehaviour
 {
     
     private TouchControl touchControl;
     
     public void DropZoneStartPos()
     {
         touchControl = new TouchControl();
         
         if( gameObject.layer == LayerMask.NameToLayer( "Upper Container" ) )
         {
             
             if( gameObject.transform.position.x == touchControl.dropZoneArr[0].transform.position.x )
             {    
                 
                 touchControl.dropZoneArr[0].transform.position = new Vector2( gameObject.transform.position.x, gameObject.transform.position.y + 2.5f );
                 Debug.Log(touchControl.dropZoneArr[0].transform.position);
             }
         }
     }
     
     void OnTriggerEnter( Collider other )
     {
         
         if( other.gameObject.layer == LayerMask.NameToLayer( "Upper Container" ) && other.transform.position.y > gameObject.transform.position.y &&
             other.gameObject.tag != "Drop Zone" )
         {
             
             gameObject.layer = LayerMask.NameToLayer( "Lower Container" );
         }
     }

}

The NullReferenceException is on this line: if( gameObject.layer == LayerMask.NameToLayer( "Upper Container" ) )

This script is attached to many game objects that either have a "Lower Container" layer or a "Upper Container" layer attached to them. I manually attach the layers in the editor. When the game starts another script references this script and the error occurs.

What I find really strange is that there is no problem when the OnTriggerEnter() runs on this line: if( other.gameObject.layer == LayerMask.NameToLayer( "Upper Container" )

Does anybody have an idea of what is happening?

Thanks in advance.

Comment
Add comment · Show 5
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 FWCorey · Mar 05, 2013 at 06:17 AM 0
Share

Why slow the game down searching by name anyway? Just use the index for the layer or if it's purely for legibility, use an enum or constant to reference the layer index.

avatar image highpockets · Mar 05, 2013 at 04:39 PM 0
Share

Cause I'm a total noob and I don't know any better. Haha. What's the best way to switch between layers/tags and check if they're attached to game objects at runtime for optimization? I imagine bitmask, but I'm a little unfamiliar. Would it solve this null reference exception ordeal?

avatar image paulaceccon · Mar 05, 2013 at 05:44 PM 0
Share

Have you tried this.gameObject.layer = Layer$$anonymous$$ask.NameToLayer( "Lower Container" );

avatar image highpockets · Mar 05, 2013 at 07:42 PM 0
Share

Yes, but I get the same problem

avatar image highpockets · Mar 05, 2013 at 08:52 PM 0
Share

I keep trying different approaches, but I can't seem to solve my problem.

Does anybody have any idea?

Any help would be much appreciated

1 Reply

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

Answer by hoy_smallfry · Mar 06, 2013 at 03:49 AM

Usually, you get a NullReferenceException when you are trying to access a member with . on an object, when the object is null, like this explains.

That line that was pointed out to you by the compiler is pretty solid, especially since gameObject can't be null, and neither can layer since Unity makes sure that a layer has to be specified.

I think maybe it has nothing to do with that line, but the code surrounding it. The only line I see directly surrounding it that is dereferencing an object is this one:

 gameObject.transform.position.x == touchControl.dropZoneArr[0].transform.position.x

Again, gameObject has to exist, and so does transform, position, and x, since those define a bare-bones game object.

So mostly likely, that leaves these two guys: touchControl, dropZoneArr[0], but touchControl is defined right above!, so it's gotta be dropZoneArr[0].

Since you are not getting an IndexOutOfRangeException (explained here), can't be because your container is empty.

I guarantee you its because the first element of dropZoneArr is null, or something bad is happening in TouchControl's constructor.

Print them out right before that if statement to see which one is your problem child:

 Debug.Log(gameObject.name + "\'s dropZoneArr[0]: " + touchControl.dropZoneArr[0]);

 if( gameObject.transform.position.x == touchControl.dropZoneArr[0].transform.position.x ) 

Comment
Add comment · 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 highpockets · Mar 06, 2013 at 05:26 AM 0
Share

You're my hero. I didn't use the GetComponent() to declare touchControl, therefore, I was never getting the script, hence NULL!!! Thanks a ton for pointing me in the right direction

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

13 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

Related Questions

Best practice handling deleted object refences during SceneManager.LoadScene 0 Answers

Problem with checking if null 1 Answer

Help with fixing NullReferenceException 3 Answers

How do I nullreference this correctly? 1 Answer

Why does ScriptableObject.CreateInstance, where T is definite at runtime, return null? 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