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 StewVanB · Jul 08, 2013 at 07:29 PM · gameobjectnullreferenceexceptiondebug

this.gameObject null reference exception

Hello, I am having an issue with this.gameObject being null. I am unsure as to why it is saying that it does not exist. It isn't an instantiated object. It is there in the editor. There aren't any Destroy calls made on these objects.

Here is some of the code:

 public void UpdateCamera(){
     switch(controllingPlayer){
     case playerSlot.AI:
         break;
         
     case playerSlot.One:
         gameCameraScript.SetPlayer(1,this.gameObject);
         break;
         
     case playerSlot.Two:
         gameCameraScript.SetPlayer(2,this.gameObject);
         break;
         
     case playerSlot.Three:
         gameCameraScript.SetPlayer(3,this.gameObject);
         break;
         
     case playerSlot.Four:
         gameCameraScript.SetPlayer(4,this.gameObject);
         break;
         
     default:
         Debug.Log("Something broke in camera.");
         break;    
     }
 }


The error occurs for any of the "this.gameObject" references.

and,

This is the SetPlayer method being called here:

 public void SetPlayer(int _playerNumber,GameObject _playerObject){
     switch(_playerNumber){
     case 1:
         playerOne = _playerObject;
         break;
         
     case 2:
         playerTwo = _playerObject;
         break;
         
     case 3:
         playerThree = _playerObject;
         break;
         
     case 4:
         playerFour = _playerObject;
         break;
         
     default:
         break;
     }
 }

Does anyone know why this may be happening now? And hasn't since 6/26.

Comment
Add comment · 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 StewVanB · Jul 09, 2013 at 06:17 PM 0
Share

In case it helps the error is:

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

3 Replies

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

Answer by StewVanB · Jul 08, 2013 at 08:00 PM

Ok, so i went to lunch and when I returned this is no longer an issue. It just "works" again. No code changes, perhaps this is a bug in this version of unity?

Comment
Add comment · Show 2 · 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 StewVanB · Jul 09, 2013 at 02:05 PM 0
Share

Sorry about all the posts on this topic. When I got started working on my project again this morning, the issue has started once again. This is really putting a damper on my productivity. Should I contact unity support about this issue?

avatar image Kiloblargh · Jul 09, 2013 at 07:26 PM 0
Share

Yes; send them your project with the built-in bug reporter.

avatar image
0

Answer by Filippo94 · Jul 09, 2013 at 02:07 PM

Can't you just use gameObject?

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 StewVanB · Jul 09, 2013 at 02:10 PM 0
Share

I would think so, however all of these return null:

gameObject,
this.gameObject,
transform.gameObject,
this.transform.gameObject, etc.
basically all references to gameObject
avatar image
0

Answer by cerebrateB · Aug 20, 2015 at 09:41 PM

Okay, So I know that this question is years old, but I'm leaving my answer here just in case someone else stumbles across the same issue like I did.

 Debug.Log("This gameObject is: " + this.gameObject); // <= this works
 ship.SendMessage("AddShipComponent", this.gameObject); // <= this doesn't

I kept getting null reference errors on the line with the send message, despite the debug log line printing out what I would expect it to. After a couple days of pulling my hair out, I finally moved the script in question to execute after default time in the scrip execution order. After doing so, everything worked as expected.

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 arthur_unity993 · Aug 06, 2020 at 09:25 AM 0
Share

I know this is very old, but the likely reason that script execution order fixed it for you is because ship was null. Changing the execution order caused the ship to get initialized early enough that you no longer see the error. So its not really an answer to op

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

18 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

Related Questions

NullReferenceException while trying to access script on object 2 Answers

Using Scripts in AssetBundles 0 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

NullReference when accessing GameObject in array (C#) 1 Answer

How to form copies of yourself that follow you when you walk? (2D Platformer) 2 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