Name.Contains for components

Hi guys,
I’m fairly new to this (a years experience) so be gentle with me okay?


I’m making an RPG at the moment where the player is able to control several characters at once. I’m using a script on an empty GameMaster object to manage control of the characters which is all working fine.

//The Problem

I need to reference the characters class script (e.g. “WarriorClass.cs”, “RogueClass.cs”, etc…). They all have systematic naming in the hopes that I might be able to search for a part of the name.

//My Miserable Attempt

MonoBehaviour characterClass = selection.GetComponent <name.Contains ("Class")>();

(selection is my reference to the object)
the problem is obviously that name is a reference to the gameObject and not to the component.

Is there a way I can access a component without knowing it’s specific name or via any other information?

Thanks guys.

~~ also c#

Use inheritance. Pseudo code

public class CharacterClass : MonoBehaviour {
    // Any features common to all classes should be here

public class WarriorClass : CharacterClass {}
public class RougeClass : CharacterClass {}
// ....

// To get the component use

CharacterClass characterClass = GetComponent<CharacterClass>();

Using inheritance and polymorphism like this is fundamental to object orientated programming. Google it, you’re in for a whole new world of possibilities.