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
1
Question by Elit3d · Jan 02, 2015 at 02:01 AM · gameobjectgetcomponentgameobject.findgetcomponents

GameObject.Find GetComponent C#

Just wondering why this isnt working:

  GameObject.Find(“first_person_controller”).GetComponent<MouseLook>().enabled = false;

everything is named correctly. I did a debug and it prints NULL so I’m not really sure what to do from here

Error message:

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

Comment
Add comment · Show 8
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 jmgek · Jan 02, 2015 at 02:24 AM 0
Share

Try getting your FPC first then get it's component. Also you should be finding gameobjects by name starts with, it's better practice.

avatar image WillNode · Jan 02, 2015 at 02:41 AM 0
Share

can you give a screen shot of the object you are looking for in hierarchy?

avatar image Elit3d · Jan 02, 2015 at 04:25 AM 0
Share

http://gyazo.com/2f452401580c3fd2cf8b19d2a369fd04

http://gyazo.com/71cd2842726560cfe90c4ca15581f2f7

avatar image BMayne · Jan 02, 2015 at 05:58 AM 1
Share

I would suggest NEVER using GameObject.Find. It is super slow and is error prone. Use Tags or have a class that keeps track of them.

avatar image Elit3d · Jan 02, 2015 at 02:48 PM 0
Share

@B$$anonymous$$ayne would you suggest GameObject.FindGameObjectsWithTag

Show more comments

4 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by WillNode · Jan 02, 2015 at 05:57 AM

try to make sure that the Game Object ( “first_person_controller” ) are active when the game trying to execute that code. this is because GameObject.Find function only returns active Game Objects.

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 Elit3d · Jan 02, 2015 at 02:49 PM 0
Share

it is active

avatar image
0

Answer by tanz94 · Jan 02, 2015 at 02:39 AM

First make sure first_person_controller name is same as seen in hierarchy.And then check in an if statement whether the specified component is attached to it.

if( GameObject.Find(“first_person_controller”).GetComponent()!=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
avatar image
0

Answer by YouBungy · Aug 01, 2018 at 06:39 PM

You Cant Just Use "Find". The Best Way Would Be To Use set a tag on the Object you want to find and use "FindWithTag" instead of "Find". Works Every time.

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 Bunny83 · Aug 01, 2018 at 06:42 PM 0
Share

Uhm, what makes you think that you can't use GameObject.Find?

avatar image YouBungy · Aug 03, 2018 at 02:09 PM 0
Share

I guess I should say you should use. Gameobject.find is getting replaced with the new 2018 update as shoe in their live stream

avatar image
0

Answer by cryingwolf85 · Aug 03, 2018 at 04:49 PM

I see what the problem could potentially be.

In one of the screenshots you posted, I see there's an arrow pointing down on the "first_person_controller" object, indicating it has children.

Make sure the MouseLook script is attached to the object first_person_controller and NOT its children, or else you will get a null with you current code.

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

32 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

Related Questions

How to look for a game object and a change some components on that object? 1 Answer

Find GameObjects with a true boolean and put them in an array? 1 Answer

Getting errors when referencing a variable. 1 Answer

Find one inactive player (gameobject) 2 Answers

GetComponent error 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