How to make a class that can be accessed from any script in a project.
Sorry if this example is kinda vague, I'm not the best with these ask board sites. So say I have an empty game object called "GameManager" with a script also named "GameManager", and just to make things simple, another game object named "Player", with a script called "PlayerData". But both the scripts "GameManager" and "PlayerData" require using a method called "Gen()" in a class called "tools". Where and how would I declare the class "tools", do I have to attach it to a game object or can it just be in an assets folder? And how would I even call the method?
What the methods do or other classes need is irrelevant, I just need to know how to set the class up and access it.
Answer by OneCept-Games · Jan 12, 2018 at 10:11 PM
You could create your Tools
class as public
and your Gen()
function in this Tools
class as static
.
Something like this Seconds To Time converter function.
using UnityEngine;
public class Tools : MonoBehaviour {
public static string Gen(double secs)
{
System.TimeSpan t = System.TimeSpan.FromSeconds(secs);
string answer;
if (t.TotalMinutes < 1.0) {
answer = string.Format("0:{0}", t.Seconds);
} else if (t.TotalHours < 1.0) {
answer = string.Format("{0}:{1:D2}", t.Minutes, t.Seconds);
} else { // more than 1 hour
answer = string.Format("{0}:{1:D2}", (int)t.TotalHours, t.Minutes);
}
return answer;
}
}