• 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 /
  • Help Room /
avatar image
Question by fimaaa · Jan 31, 2018 at 12:48 PM · gameobjectbuttongetcomponentnull reference exception

getcomponent cant find every button script in every gameobject

so yesterday my code is working, but now its error. i found the problem is the unity cant find button object, i dont know why so i erase all code and make something simple like in image alt text

but then program still print null, and the object is founded. what is wrong with my code,the name is already correct.

Try to change to Text,unity can find it, but every button is always null. what is wrong with this situation, and can i get the solution thank you :) (sorry if my english is bad)

situasion.png (100.3 kB)
Comment

People who like this

0 Show 2
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 meat5000 ♦ · Jan 31, 2018 at 02:43 PM 0
Share

Start by simplifying it. You declared and set mybutton but instead of using mybutton you keep using GameObject.Find().GetComponent().

The problem could be the way you nullcheck. If I remember right, to check the presence of an object you use if(object) instead of if(object == null). Not got time to look up about Components and scripts; school run.

avatar image fimaaa meat5000 ♦ · Jan 31, 2018 at 03:04 PM 0
Share

so to check null is like that :) so i try to check null and it's still null.(i relize it's still null)

my real problem still there.when i use mybutton.enabled = false, it's still nullreferenceexception. alt text

and even when the gam is played,mybutton not filled ( i also cant drag the button to mybutton manual)

capture.png (29.1 kB)

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by victorbisaev · Feb 01, 2018 at 08:32 PM

Try this:

1) Call GameObject btnGameObject = GameObject.Find("/Canvas/Pause_Button"); and inspect btnGameObject so it should be not null. It checks the GameObject with this name is in the hierarchy. Notice "/" at the beginning of the string ID "/Canvas/Pause_Button" so to perform the search from the Root.

2) When btnGameObject is not null, perform Button btn = btnGameObject.GetComponent<Button>() and inspect btn is not null. It checks the "Button" component is attached to the "/Canvas/Pause_Button" GameObject.

Your code looks good so I think the problem is in the ID or missed component.

Comment

People who like this

0 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 fimaaa · Feb 02, 2018 at 12:26 AM 0
Share

if i check the game object GameObject btnGameObject = GameObject.Find("/Canvas/Pause_Button");unity found it, but when i perform the button Button btn = btnGameObject.GetComponent<Button>() it's back to null. alt text

i'am so confuse, i change to find component like Text, it's not null. only button who get null(every button in this project tbh)

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

118 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 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 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 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 change image of button when clicked? 2 Answers

access object from another 2 Answers

How to keep bool true even when other method try to set it off 0 Answers

Generate ladder on y axis 0 Answers

Activate when mouse klick object 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