- Home /
The question is answered, right answer was accepted
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?
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.
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.
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)
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?
You haven't attached both scripts, have you?? Only the child class needs to be attached to the gameObject
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?
I also know that each object will only have one subtype-interaction script: never 0 or more than one.
Follow this Question
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