- Home /
How do I get the system type of Mathf in JS ?
I am attempting to call a function in Mathf by utilizing its string name, which requires the use of the Invoke function. However, before I can do this, I need to get the type of Mathf:
var mathLibrary : System.Type = System.Type.GetType("System.Type.Mathf");
However, no matter if I use "Mathf", "System.Mathf" or even "Type.Mathf", this function always returns null. What is the correct syntax ?
Thank you in advance and have a nice day
Answer by fafase · May 10, 2015 at 04:21 PM
So first off Mathf is not from System, which is one reason it does not find it. Math is in System but Mathf is in UnityEngine.
Second, it seems GetType requires extra info when dealing with custom classes/structs. By custom I mean anything that is not in .NET. Here is what it wants: "UnityEngine.Mathf, UnityEngine"
So this should go fine (it does for me):
var mathLibrary : System.Type = System.Type.GetType("UnityEngine.Mathf, UnityEngine");
Debug.Log(mathLibrary == null);
Awesome, that did it, thank you so much, this was driving me crazy since I couldn't find any documentation on $$anonymous$$athf's location. Could you please indicate where you got that info ? It would be a huge help for future research
One more question, first I am checking if the method exists in $$anonymous$$athf, but when I test with $$anonymous$$ax, since it has 2 overloads, I get an ambiguity exception. But solving the ambiguity would involve evaluating the parameters in my custom parsing system (which, in addition to text expressions, also parses a custom class that extends $$anonymous$$onobehaviour) before trying to find the function, which seems very wasteful, is there an alternative ?
@$$anonymous$$aiserAfini:
If a method has one or several overloads the compiler has to pick the right one based on the passed parameters. There's no way around that. Or how should you pick the right "$$anonymous$$ax" method if you don't know if you're going to pass integers or floats.
btw: if you actually hardcode that line in your question you don't have to use System.Type.GetType. Just use
// UnityScript
var mathLibrary : System.Type = $$anonymous$$athf;
// C#
System.Type mathLibrary = typeof($$anonymous$$athf);
Concerning how I know that $$anonymous$$athf is not .NET, well because it cannot be found there :) and also because .NET already has $$anonymous$$ath so there would be little reason to have a $$anonymous$$athf there. Actually, $$anonymous$$athf is mostly wrapping $$anonymous$$ath class most likely with some specific considerations for Unity.
Finally, a good way to know about classes and structs, the documentation may give hints or do a right click on the class and select Go to Declaration. It will open the assembly browser containing some extra info. $$anonymous$$ost of the time, it contains only the big idea of a class as most of it is in C++.
If you want to know how a certain class is implemented, just download ILSpy and open the UnityEngine.dll. ^^
Your answer
Follow this Question
Related Questions
How to loop a function? 2 Answers
Reopening a project causes it to raise a ReflectionTypeLoadException 0 Answers
A small spawning challenge.. 5 Answers
iOS Reflection Code stripping problem 2 Answers
MathF clamp not working 1 Answer