Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Trying to get a non static method from anothor class.

Discussion in 'Scripting' started by Decoder46, Jun 13, 2015.

  1. Decoder46

    Decoder46

    Joined:
    Jun 12, 2015
    Posts:
    20
    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:
    Code (CSharp):
    1. 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 :/

    sorry for my bad english :p
     
  2. HiddenMonk

    HiddenMonk

    Joined:
    Dec 19, 2014
    Posts:
    987
    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?
     
  3. Decoder46

    Decoder46

    Joined:
    Jun 12, 2015
    Posts:
    20
    Forgot to say the error is
    Code (CSharp):
    1. GetComponent<NetworkManeger>().SpawnPlayer();
    The method:
    Code (CSharp):
    1. public void SpawnPlayer(){
    2.         Network.Instantiate (playerPrefs, new Vector3 (1, 4, -2),Quaternion.Euler(new Vector3(0,270,0)),0);
    3.  
    4.     }
     
  4. HiddenMonk

    HiddenMonk

    Joined:
    Dec 19, 2014
    Posts:
    987
    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<NetworkManeger>().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.
     
    Last edited: Jun 13, 2015
  5. Decoder46

    Decoder46

    Joined:
    Jun 12, 2015
    Posts:
    20

    Yes, thank you! how i did not thought about it :D
     
    HiddenMonk likes this.
  6. HiddenMonk

    HiddenMonk

    Joined:
    Dec 19, 2014
    Posts:
    987
    I only thought of it because I was in your position once ^_~
     
    Decoder46 likes this.