Z
Z
Zimaell2020-07-04 12:33:14
Unity
Zimaell, 2020-07-04 12:33:14

How to connect a class without an object on the stage?

I have a Server class, it has a SendData method, there is a Menu class, it has a Click1 method, how can I call the SendData method of the Server class in the Click1 method, given that the Server class is not hung on anything, and the Menu class is hung on the button which the Click1 method is called when clicked.

public class Server : MonoBehaviour{
  public static void SendData(){
    StartCoroutine(SendDataWWW());
    }
  IEnumerator SendDataWWW(){
    ..........
    }
  }

public class Menu : MonoBehaviour{
  GameObject obj;
  Server srv = obj.GetComponent<Server>();
  public void Click1(){
    srv.SendData();
    }
  }

but that doesn't work...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
freeExec, 2020-07-04
@Zimaell

Remove inheritance from MonoBehaviourand create an instance of the class throughServer srv = new Server();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question