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 mthicke2 · Oct 29, 2014 at 04:39 AM · gameobjectreference

Setting an object reference to null

I have a reference to a gameObject. At the start of the game it is null - during the game it is assigned to different gameObjects as needed. I wan to be able to test if it is null or set to whatever gameObject. The problem is I get an error 'Object reference not set to an instance of an object' when it is null. Why cant I have a null object reference?

Comment
Add comment · Show 3
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 SamuZen · Oct 29, 2014 at 04:54 AM 2
Share

Are you testing GameObjects , just in case it is not null ?

GameObject obj = null;

if(obj != null){ //here you test with other Game Object }else{ //here you already know that the Game object is null }

avatar image richyrich · Oct 29, 2014 at 09:32 AM 0
Share

As @SamuZen implies through code suggestions and also from your error message, it seems you are accidentally still calling methods of an object, set to null at the time, which as you know is not permitted. For sanity check and confirmation, yes you can have objects set to null.

avatar image screenname_taken · Oct 29, 2014 at 10:32 AM 0
Share

You get the error message if it's null, because the game will try to do something to something that isn't there. It's not intelligent, if you don't tell it to check first, it won't check. Look at SamuZen's code, it's the right way of doing it.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by bubzy · Oct 29, 2014 at 10:47 AM

as mentioned in the comments you can run an if statement or you can do something like the example below

 GameObject tempGO = new GameObject();
 GameObject yourGO = new GameObject();
 
 
 void Start()
 {
 tempGO.tag = "tempGO"; //in case you need to check the gameobject by tag
 }
 
 void Update()
 {
 //assign your gameobject here
 //when you unassign it you can use
 yourGo = tempGo;
 //this way the gameObject will never be null
 }
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

30 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 avatar image

Related Questions

How to get a List of references of instances in a static variable 1 Answer

Can't re-enable a gameobject 1 Answer

Reference to Object error but script works fine 1 Answer

Problem passing a GameObject to another script 0 Answers

calling a script that starts a number. 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