hello.I have a monobehaviour class named world and I want to use it’s functions in another class.at first I made all functions static because I didn’t want to make any instance of my World class but I figured out that it’s not good in unity(a problem for example, was that I wanted to use some transforms in my World class and since they had to be declared static I couldn’t set them in the inspector panel).how can I access world’s functions using gameobject.find().getcomponent() or other ways?I made an obiect in World named w=(World)(GameObject.Find(“World”).GetComponent()); but it has errors.please help me.
I guess you’re using C#? (just guessed from your World cast). You should take a look at the docs on “how to access other GameObjects”.
In your case it seems that your World script is a kind of a singleton. If that’s the case there should be always only one instance in your scene. You can use a static variable to hold a reference to this instance so it can be accessed from everywhere.
public class World : MonoBehaviour
{
private static World m_Instance = null;
public static World Instance
{
get
{
if (m_Instance == null)
m_Instance = (World)FindObjectOfType(typeof(World));
return m_Instance;
}
}
void Awake()
{
m_Instance = this;
}
[... the rest of your class ...]
public Vector3 worldSize;
}
Now you can access the World class instance from every other class (doesn’t need to be a MonoBehaviour class).
Debug.Log(World.Instance.worldSize);