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 /
This question was closed Mar 20, 2021 at 11:45 PM by GetMeSomeGames for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by GetMeSomeGames · Mar 20, 2021 at 08:30 PM · scripting probleminheritanceaccessing scripts

How do I best access a child script through a parent script when using inheritance

I'm interested in creating an interaction system, in which I want the player to be able to start one out of a list of interaction types with the object it is currently looking at (only one type of interaction per object).

It is currently already possible to let my player detect if an interaction is possible and send a message to a generic "Interaction" script in order to start this interaction. I would like to use a separate script for each type of interaction (for example "I_ThrowObject"), since there will be a lot of them. These scripts will then inherit from the "Interaction" script.

The part that I am stuck on is this: How do I access the methods of the child interaction class (like the example "I_ThrowObject"), without having to manually set a reference to the child inside the parent ("Interaction")? Does a generic class variable exists to which I can write the reference from the child script to the parent script? Or should I handle this type of script interaction differently altogether?

Comment
Add comment · Show 9
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 Hellium · Mar 20, 2021 at 09:16 PM 0
Share

I am not sure to understand.

Let's suppose your base class ( Interaction ) has a public abstract / virtual method called void Interact(), your child class ( I_ThrowObject ) should also have it and implement the custom behaviour you want.

You don't need a direct reference to your child class. You only need to call targetObject.GetComponentInChildren<Interaction>().Interact();. The child class will be returned by GetComponent without any issue.

avatar image GetMeSomeGames · Mar 20, 2021 at 09:42 PM 0
Share

alt text

Hopefully this image clarifies my situation for a bit. I can't directly find the Throw_Interaction script as it could also be named differently, as long as it is a child of the Interaction class. If I run the Interact() method from the Interaction script, it does not run the override Interact() method in its child because of the inheritance structure.

interactionproblem.png (17.3 kB)
avatar image Hellium GetMeSomeGames · Mar 20, 2021 at 10:29 PM 1
Share

Overridden methods precisely solves the problem you are describing. If the method is overridden in the child class, it is supposed to be called rather than the base class' one.


Having some code would help (please, edit your question instead of publishing an answer / comment)

avatar image GetMeSomeGames · Mar 20, 2021 at 11:12 PM 0
Share

I am sorry for the reply instead of making a comment and thank you for trying to help me.

In the picture below is the important chunck of the code on the objects side. I understand that if I were able to call the Interact() function in I_HoldObj, that it would also run the parent version of that function (in the Interaction script). But I would like to do it the other way around: I know that every interactable object in my scene will have a Interaction script, but each object has a different sub-interaction script, each with different names. Is there a way create a connection between the generic Interaction script and the specific subtype-interaction scripts so that I only have to call the generic one from my player to also run the subtype one?

alt text

interactionproblem2.png (51.3 kB)
avatar image Hellium GetMeSomeGames · Mar 20, 2021 at 11:17 PM 1
Share

You haven't attached both scripts, have you?? Only the child class needs to be attached to the gameObject

avatar image GetMeSomeGames Hellium · Mar 20, 2021 at 11:21 PM 0
Share

I tried that but how can I then call the specific subtype-interaction script without knowing which one it is on the currently selected object? The only method I would know is to make a big switch case that checks for every subtype if there is a script present for it. Is that really the most elegant way of solving this?

Show more comments
Show more comments
avatar image GetMeSomeGames GetMeSomeGames · Mar 20, 2021 at 11:18 PM 0
Share

I also know that each object will only have one subtype-interaction script: never 0 or more than one.

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

208 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

To count deaths with global variable 1 Answer

Inheriting Parent Rotation and position 2 Answers

How to store data in script and attach it later? 0 Answers

Inheritance Implementation Weapon add System for the Pro Military: FPS Character? 0 Answers

An OS design issue: File types associated with their appropriate programs 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