Accessing/Referencing Child Script Via Parent Script.

In a nutshell, I would like to reference a ‘child script’ on an object using it’s ‘parent script’. Inheritance

a ‘m14G’ is a ‘Weapon’ is a ‘Item’

Example of what I’m doing:

I have a ‘reference’ object on my character that is the parent object for my weapon “The hand”.

The script concerning the weapon is currently on an child object of the weapon, that I have named triggers.

This object is disabled whenever The weapon{item} is not in use via a complex Inventory script that is not very OOP friendly:? as I have to extensively reference and re-reference everything.

The script however is directly taking ‘input calls’, Hench disabling triggers when not active. I’m doing this over disabling the entire object, as the same object is used visually as well.

What I Want To Do: Example

Instead of skull splitting referencing, I would like to be able to reference the child script via the parent script.

a ‘m14G’ is a ‘Weapon’ is a ‘Item’

Can I get to ‘m14G’ using ‘Weapon’ so as to not get stuck using if/case blocks?
“or is this all ready accomplished with overriding?”

"To clarify. Each Weapon has its Own Script Inheriting From ‘Weapon’, ‘Weapon’ Inheriting from ‘Item’.

I would like to know if this is:

  1. Is it Possible and could you please give an example?
  2. Is it at all a part of Overriding?
  3. Is it Practical?
  4. Must i make a new q for each question?

C# If Possible, Thanks :slight_smile:

Some Notes:

This is not a traditional FPS. More a FPRPG ‘SURVIVAL’, None Horror!

I will be willing to give some sample code if needed. Might take a week to condense the 300 line inventory script, without breaking it, though :/.

Not willing to share complete code - Hella paranoid :S

I have looked at Official Tutorials- I think I’m just missing the obviouse.

Please don’t Reference Asset Store Items and tell me to buy them. I will hate you for ever. This is a simple coding method Q xD.

Thanks for having a look at this Q.

1st - Updated Q’s :O.
2nd - Fixed Main Question :open_mouth: and added some stuff.

Apologies :smiley:

Is it Possible and could you please give an example?

No it is not possible for a base class to know about a sub class. A dog is a mammal but why should mammal know about how a dog eat? Same in OOP

Is it at all a part of Overriding?

Yes, using virtual and overriding will solve your issue. A dog is a mammal, all mammal need to eat so mammal would have a virtual eat method that is overriden if necessary. Most likely Mammal is abstract and so is the method so the sub class has to implement it. A IEater interface would be appropriate too.

Is it Practical?

It is the base of OOP.

Must i make a new q for each question?

I guess I answered them all.

In your case, I would make Item and Weapon as abstract and mg42 would implement all abstract methods. This enforces also the fact that an item is nothing if not something else. Making it abstract prevent the creation of a non entire object.