Trying to get a non static method from anothor class.

Hi all I new to unity so if i miss something plz tell me and i will add :).

I have a class called NetworkManeger and I am trying to get a method called SpawnPlayer from anothor class using this code: GetComponent<NetworkManeger>().SpawnPlayer();

and i am getting this error:

-The two scriptes are in the same object (Main Camera).
-This still spawn tha player although he is throwing that error :confused:

sorry for my bad english :stuck_out_tongue:

Might need more info, where is the error pointing to?
Is GameManager using that code in its start method and its throwing the error?
If it spawned the player, then maybe this is not the problem. Double click the error message, does it bring you to that line of code?

Forgot to say the error is
GetComponent<NetworkManeger>().SpawnPlayer();

The method:

public void SpawnPlayer(){
        Network.Instantiate (playerPrefs, new Vector3 (1, 4, -2),Quaternion.Euler(new Vector3(0,270,0)),0);

    }

Just to be clear.
You have a script called NetworkManager (or NetworkManeger) with a method on it called SpawnPlayer.
You have a script called GameManager and in its start method you are doing “GetComponent().SpawnPlayer();”

One player still spawns, but you also get an error basically saying “cannot find component NetworkManeger”.
Perhaps you have another gameobject that also has a GameManager script on it, but not a NetworkManeger script. This can explain why the player does spawn, since you have everything set up correctly on one gameobject, but on another gameobject you might have forgotten to take off the GameManager script.

Yes, thank you! how i did not thought about it :smile:

1 Like

I only thought of it because I was in your position once ^_~

1 Like