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 Jan_Julius · Sep 19, 2014 at 11:00 AM · raycastparentchildcomponent

How can I access a component in a parent object?

Hello.

I'm raycasting on an object whom has a parent and I want to access a component on the parent, how do I do this?

this code:

 void Update () {
         Cylinder otherobject;
 RaycastHit hit;
         Vector3 angle0 = transform.TransformDirection(Vector3.up);
         if (Physics.Raycast(transform.position, angle0, out hit, 1)){
             otherobject = hit.collider.gameObject.transform.parent.GetComponent    <script>().something();
 }
 }

How can I access a function on the script? the error I get: Cannot implicitly convert type void' to Cylinder'

How can I fix this?

Thanks!

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

1 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by dmg0600 · Sep 19, 2014 at 11:13 AM

That error shows that the method "something()" is not returning anything as it is declarated like this:

 public void something() {
     //code
 }

Just be sure that the method you are using from the parent returns a Cylinder.

The way you are accessing the component in the parent is right although I would just get it as follows:

 if (Physics.Raycast(transform.position, angle0, out hit, 1))
 {
     otherobject = hit.transform.parent.GetComponent<script>().something();
 }
Comment
Add comment · Show 4 · 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 Jan_Julius · Sep 19, 2014 at 11:26 AM 0
Share

But what I want is basically when the raycast hits the object I want the parent object to do something for example destroying itself, how can I accomplish this?

avatar image dmg0600 · Sep 19, 2014 at 11:32 AM 0
Share

Just call the method without storing the return value in a variable. Like this:

 void Update () {
     RaycastHit hit;
     Vector3 angle0 = transform.TransformDirection(Vector3.up);
     if (Physics.Raycast(transform.position, angle0, out hit, 1))
     {
         hit.transform.parent.GetComponent<script>().something();
     }
 }

The problem was you were trying to store the return value of the method "something()" into your variable otherobject which wasn't posible as "something()" didn't return anything.

If this solves your problem please mark it as answered.

avatar image Jan_Julius · Sep 19, 2014 at 11:54 AM 0
Share

But now it tells me "otherobject" doesn't exist?

avatar image dmg0600 · Sep 19, 2014 at 12:26 PM 0
Share

Right, sorry, I just editted my comment above. Now it should work.

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

25 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

Related Questions

Make a simple tree 1 Answer

How can I differentiate between colliding with an object or its child? 1 Answer

Raycasting from child weird behaviour 2 Answers

Instantiate as a child? 3 Answers

Raycast only returning parent object; need child hit 3 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