Using method with SOME parameters as argument

Hello guys.

I have a problem that I can’t solve, even after trying to find the solution myself and with similar topics.

// First script

public void CreateNewCard(string aName, ?? method(someParameters)) {
    // Do something
}

public void Method1(string someText, int someValue, float number) {
    // Do something
}

public void Method2(int anotherValue, int something) {
    // Do something
}


// Second script

GetComponent<FirstScript>().CreateNewCard("Name", GetComponent<FirstScript>().Method1("Text", 5, 1.5f));
GetComponent<FirstScript>().CreateNewCard("Name2", GetComponent<FirstScript>().Method2(4, 3.7f));

As you can see, i want to use differents methods with differents number and type of parameters as argument, because I want to store a method in new objects (here it’s cards), so those cards can use the method later. But I don’t know how (I can’t with delegate or Action because of the differents number and type of parameters).
I don’t want to create prefab with the script + method on it, because i can have more than 1000 types of card, and i don’t to have so much prefab.

Thanks for the help, and sorry for my english, it’s not my native language :slight_smile: !

Without delving into more of what you’re trying to accomplish just yet, are you maybe looking for the params designation/keyword? Method Parameters - C# reference | Microsoft Learn

If you explain a bit more about your situation, you might be able to get some more detailed explanation/responses (other ways to try things - just maybe).

1 Like

You want to bind the arguments when you call CreateNewCard? As in they’ll never change? Then it’s easy:

public void CreateNewCard(string aName, Action method) {
    // Do something
}

GetComponent<FirstScript>().CreateNewCard("Name", () => GetComponent<FirstScript>().Method1("Text", 5, 1.5f));
GetComponent<FirstScript>().CreateNewCard("Name2", () => GetComponent<FirstScript>().Method2(4, 3.7f));

This assumes that on the first card you create, you always want to call Method1 with the arguments “Text”, 5 and 1.5f. Seems like that’s what you’re looking for?

2 Likes

Thanks guys !

@methos5k : params is a good thing i dnd’t know !

@Baste : it works ! I didn’t know the use of () => could solve the problem. It was so easy, and i search for hours.

Cool, glad ya got it resolved :slight_smile: