hey C# people what am I doing wrong?
So in GameMaker there's a really handy function called choose() which takes in any number of parameters and returns the value of one of the parameters at random. For example, if I wanted magic 8 ball to give an answer, I could write choose("signs point to yes", "no", "maybe") and get one of those three strings returned to me. Now I couldn't find anything in Unity that did exactly the same thing, so I set out to write the function myself. Initially I wanted to extend the Mathf class because at the time I thought Random() was in the math class (it is in some languages), but I guess you can't do that because it's a static class already. So I made a bonus Math class called Meth. I know, super classy! This is my code:
I haven't debugged the code yet but that's not where I'm having problems. At least I don't think it is. Here's a stub of the code that's throwing an error:
The error I'm getting is "error CS0103: The name `Meth' does not exist in the current context". In MonoDevelop, Meth.Choose is appearing in red but using Methlab; shows fine which to me means that everything from that namespace should be fair game. Does anyone have an idea what the problem is?
Thanks!
Because the class has a generic parameter, you need to tell it what type you want for the T when you're calling it, so
Code:
Meth<string>.Choose("1", "2", "3")
Also what you're passing in won't (or shouldn't I think) be an array. You either need to make "1", "2", "3" an array, ooor you can change the choose method to:
Code:
public static T Choose(params T[] options)
That will work with any number of parameters now, which I think is what you're aiming for?
Also there might be an issue with not all paths returning a value in the Choose method... possibly.
Hope this helps!