Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
This post is currently awaiting moderation. If you believe this to be in error, contact a system administrator.
This question was closed Feb 19, 2019 at 09:52 AM by Hellium for the following reason:

Duplicate Question

avatar image
0
Question by unity_XY9GfkpHMyaikw · Feb 18, 2019 at 06:39 AM · rigidbodycollision detectioninputmanagerparent-childobject-reference-error

How do I access the collider of a child object through a script on the another object?

I get an error saying "object reference not set to an instance of an object"

,The Parent Object is a Mole, The mole has 3 child objects (1) Hips (2) Body (3) Hair. The collider(and rigidbody) Is on the "Hips" child object. The Script is an Input Script For detecting "Hits on the Mole".

This is The script am using to detect Input.

public class Game_InputManager : MonoBehaviour {

 void Update()
 {
     if (Input.GetMouseButtonDown(0))
     {

         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         RaycastHit hit;  //record input

         if (Physics.Raycast(ray, out hit)) // if we are hiitting something what are we hiiting
         {
             if (hit.collider.tag == "Mole") //if it only hits the gameobject with the mole tag go forward
             {
              
             Game_MoleBehaviourmole=hit.collider.gameObject.GetComponent<Game_MoleBehaviour>();  

                 mole.SwitchCollider(0);
                mole.anim.SetTrigger("hit"); 
                 Debug.Log(hit.collider.gameObject + "got hit");
             }
         }

     }
 }

}

I get this error when I compile it : Object reference not set to an instance of an object.

Comment
Add comment · Show 7
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 Captain_Pineapple · Feb 18, 2019 at 06:56 AM 0
Share

Hey there, can you add the line of your error message? But i guess it should be something in line 14 where you write Game_$$anonymous$$oleBehaviourmole = ... It should probably say: Game_$$anonymous$$oleBehaviour mole = ...

Let me know if that helps or provide more information please.

avatar image unity_XY9GfkpHMyaikw Captain_Pineapple · Feb 19, 2019 at 06:57 AM 0
Share

Game_$$anonymous$$oleBehaviour mole is written as Game_$$anonymous$$oleBehaviour mole in my code. I made that mistake while copying and pasting it.

This is the error line NullReferenceException: Object reference not set to an instance of an object Game_Input$$anonymous$$anager.Update () (at Assets/Game_Input$$anonymous$$anager.cs:28)

[Line 28 is mole.SwitchCollider(0);]

avatar image Captain_Pineapple unity_XY9GfkpHMyaikw · Feb 19, 2019 at 07:23 AM 0
Share

is the Game_$$anonymous$$oleBehaviour on the same transform as the collider you are hitting? this error implies that mole equals to null so in other words the requested component was not found.

Perhaps a Debug.Log(hit.transform.name); might help you figure this out.

Show more comments
avatar image Hellium · Feb 19, 2019 at 09:51 AM 1
Share

Use Debug.Log( hit.collider.transform, hit.collider.transform ); to check which object has been hit. Then, use the parent and GetChild to access the desired object.

FAQ :

Some reasons for getting a post rejected:

  • Posting about a specific compiling error or NullReferenceException: there is a myriad of these questions with answers already, have a look at those posts to get hints on what could possibly be your issue. Also, take a look at the Unity's support website, many errors and how to solve them are described here

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

137 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 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

Player with collider goes through another object's collider upon first collision, works only on second time 0 Answers

player going through walls even though it has colliders and rigidbodies 0 Answers

How can I detect collision between two objects when one can't have a rigidbody applied? 1 Answer

Trigger Object is still moving objects? 1 Answer

Delete the parents and give the children an rigidbody 0 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