- Home /
Question by
tobobox · Feb 08, 2013 at 06:32 PM ·
c#functionsreferencingbasics
C# utilities functions in other script
I'd like to create some utilities functions that can be access from other c# scripts, but can't figure out how to access them. I'm used to programming in c++ and those approaches don't seem to work.
For instance, I'd like to have the following function in a utilities.cs file:
public float map(float s, float a1, float a2, float b1, float b2){
return b1 + (s-a1)*(b2-b1)/(a2-a1);
}
Then in all of my other c# files I'd like to be able to just call that function as if it was part of unity... such as:
float temp = map(10,0,20,10,50);
How can this be done?
Comment
Best Answer
Answer by Dave-Carlile · Feb 08, 2013 at 06:34 PM
Create a static class, with static methods...
public static class Tools
{
public static float Map(float s)
{
// TODO : your code here...
}
}
To call it, reference the class name and the method...
float temp = Tools.Map(10);
You have to make it static or you must create an instance of the class and have a reference to that in order to call non-static methods.