I have a script for dragging around cards, and right now each card has its own method - named DoTheThing() - which does that card’s individual thing. At present these scripts each have their own if statement:
public void Update()
{
if (Input.GetKeyDown(KeyCode.Return) && isHolding.cardObject == this.gameObject)
{
DoTheThing();
gc.TurnCleanup();
}
}
And this is…fine? I guess? I mean, it works. But it seems this could be refactored onto a single script on the play area that calls the DoTheThing method from (current card sitting there) if there were a reliable way to (a) get a reference to the individual script and (b) convince the computer that the reference holds the DoTheThing method.
System.Type.GetType(GetComponentInChildren<Card>().name).DoTheThing();
This is the line of code I attempted, and I think it fulfills condition (a) but fails at (b). I’m pretty sure it is actually targeting the correct thing - The first child object it’ll find will be the card, which bears the same name as its attached script because that’s how I attach the scripts at runtime. But it won’t compile, because it can’t be assured that the type I’m grabbing is actually a script with a DoTheThing method attached. Does anyone have a clever way of getting a whole system of card scripts - all of which do their own things - to activate from a central, generalized “when this is on the play zone, press enter” command, without each card checking for that every frame?