• 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 obsidian1269 · Mar 05, 2012 at 02:58 AM · getcomponent

Assign GameObject Inspector Vars Via Script

Context: I have a desk chair in my game. This desk chair consists of two separate models (seat & base). I have an animation attached to the chair that makes only the seat spin. I want this animation to play when the user clicks on EITHER the chair OR the base. Therefore I have written a "Best Friend" script that allows me to make relationships between these objects. Whenever the player clicks on an object, it looks for any "Best Friends" and runs any animations on those best friends. It works great!

Problem: My problem is that every time I update and re-import my scene, unity erases all my inspector-made (drag&drop) connections and there are a lot of them in my scene. Therefore I am trying to automate this drag&drop relationship process. I figured I could do this easily with GameObject.Find and GetComponent in an Editor Script or at Runtime but I keep getting Null Reference Exceptions in both cases. My script is below...

 function Awake ()
 {
         // Set All Best Friends
         GameObject.Find("Chair2/Chair").GetComponent(VocabWord).bestFriendA = GameObject.Find("Chair1/Chair");
 }
Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by wrstscrnnm6 · Mar 05, 2012 at 03:38 AM

The Null Reference Exceptions most likely means that either GameObject.Find() or GetComponent() is not returning anything so I would check to make sure your syntax is correct and that your names are all set up correctly. I might also try putting your code in in Start() rather than Awake() if possible.

I would look at the various ways you can access other game objects. You can find a pretty good reference/overview here

There might be an easier way to do it, via the hierarchy perhaps.

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
1

Answer by aldonaletto · Mar 05, 2012 at 03:25 AM

Use Find at Start, not Awake - Awake is called during object creation, and some objects may not exist at that time. Another point: I have not tested this yet, but suspect that paths like "Arm/Hand/Finger1" only work in Transform.Find, not in GameObject.Find - Transform.Find only searches in the object's children, while GameObject.Find searches in all the scene objects. In the example above, use GameObject.Find("Arm") to locate the arm, then call arm.Find("Hand/Finger1").

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.

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

[SerializeField] for self components instead of GetComponent<>? 1 Answer

GetComponent(); returning null 1 Answer

How to Access Components in Children of GameObject? 1 Answer

GameObject info 2 Answers

I'm having more problems accessing a variable from another script in c# 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