Search Unity

Multiples errors, can't find the cause

Discussion in 'Multiplayer' started by AkilaeTribe, Jul 20, 2010.

  1. AkilaeTribe

    AkilaeTribe

    Joined:
    Jul 4, 2010
    Posts:
    1,149
    Greetings,

    I am currently having those errors :

    I don't understand. What does Unity expects from me ?

    Code (csharp):
    1. // Chat.js
    2.  
    3. var usingChat : boolean = false;
    4. var showChat : boolean = false;
    5.  
    6. var inputField : String = "";
    7.  
    8. var scrollPosition : Vector2;
    9. var width : int = 500;
    10. var height : int = 100;
    11.  
    12. var playerName : String;
    13. var lastUnfocusTime : float = 0.0;
    14. var window : Rect;
    15.  
    16. var playerList : Array = new Array ();
    17. var chatEntries : Array = new Array ();
    18. var chatEntriesLength : int = 4;
    19.  
    20. function Start () {
    21.     window = Rect (Screen.width / 2 - width / 2, Screen.height - height + 5, width, height);
    22.     playerName = PlayerPrefs.GetString ("playerName", "");
    23.    
    24.     if (playerName == "") playerName = "RandomName" + Random.Range (1, 999);
    25. }
    26.  
    27. function OnConnectedToServer () {
    28.     ShowChatWindow ();
    29.     NetworkView.RPC ("TellServerOurName", RPCMode.Server, playerName);
    30.     AddGameChatMessage (playerName + " has joined the chat.");
    31. }
    32.  
    33. function OnServerInitialized () {
    34.     ShowChatWindow ();
    35.     var newEntry : PlayerNode = new PlayerNode ();
    36.     newEntry.playerName = playerName;
    37.     newEntry.playerNetwork = Network.player;
    38.     playerList.Add (newEntry);
    39.     AddGameChatMessage (playerName + " has joined the chat.");
    40. }
    41.  
    42. function GetPlayerNode (netPlay : NetworkPlayer) {
    43.     for (var entry : PlayerNode in playerList) {
    44.         if (entry.playerNetwork == netPlay)
    45.             return entry;
    46.     }
    47.     Debug.LogError ("GetPlayerNode : Requested a playernode of non-existing player.");
    48.     return null;
    49. }
    50.  
    51. function OnPlayerDisconnected (netPlayer : NetworkPlayer) {
    52.     AddGameChatMessage ("A player has disconnected.");
    53.     playerList.Remove (GetPlayerNode (netPlayer));
    54. }
    55.  
    56. function OnDisconnectedFromServer () {
    57.     CloseChatWindow ();
    58. }
    59.  
    60. @RPC
    61. function TellServerOurName (name : String, info : NetworkMessageInfo) {
    62.     var newEntry : PlayerNode = new PlayerNode ();
    63.     newEntry.playerName = playerName;
    64.     newEntry.playerNetwork = Network.player;
    65.     playerList.Add (newEntry);
    66.     AddGameChatMessage (playerName + " has joined the chat.");
    67. }
    68.  
    69. function CloseChatWindow () {
    70.     showChat = false;
    71.     inputField = "";
    72.     chatEntries = new Array ();
    73. }
    74.  
    75. function ShowChatWindow () {
    76.     showChat = true;
    77.     inputField = "";
    78.     chatEntries = new Array ();
    79. }
    80.  
    81. function OnGUI () {
    82.     if (!showChat) return;
    83.    
    84.     if (Event.current.type == EventType.keyDown  (Event.current.character == "\n")  inputField.length <=0) {
    85.         if (lastUnfocusTime + 0.25 < Time.time) {
    86.             usingChat = true;
    87.             GUI.FocusWindow (5);
    88.             GUI.FocusControl ("Chat input field");
    89.         }
    90.     }
    91.    
    92.     window = GUI.Window (5, window, GlobalChatWindow, "");
    93. }
    94.  
    95. function GlobalChatWindow (id : int) {
    96.     GUILayout.BeginVertical ();
    97.     GUILayout.Space (10);
    98.     GUILayout.EndVertical ();
    99.    
    100.     scrollPosition = GUILayout.BeginScrollView (scrollPosition);
    101.    
    102.     for (var entry : ChatEntry in chatEntries) {
    103.         GUILayout.BeginHorizontal ();
    104.         if (entry.playerName == " - ")
    105.             GUILayout.Label (entry.playerName + entry.playerText);
    106.         else
    107.             GUILayout.Label (entry.playerName + " : "+ entry.playerText);
    108.         GUILayout.EndHorizontal ();
    109.         GUILayout.Space (2);
    110.     }
    111.    
    112.     GUILayout.EndScrollView ();
    113.    
    114.     if (Event.current.type == EventType.keyDown  (Event.current.character == "\n")  inputField.length > 0)
    115.         HitEnter (inputField);
    116.    
    117.     GUI.SetNextControlName ("Chat input field");
    118.     inputField = GUILayout.TextField (inputField);
    119.    
    120.     if (Input.GetKeyDown ("mouse 0")) {
    121.         if (usingChat) {
    122.             usingChat = false;
    123.             GUI.UnfocusWindow ();
    124.             lastUnfocusTime = Time.time;
    125.         }
    126.     }
    127. }
    128.  
    129. function HitEnter (msg : String) {
    130.     msg = msg.Replace ("\n", "");
    131.     NetworkView.RPC ("ApplyGlobalChatText", RPCMode.All, playerName, msg);
    132. }
    133.  
    134. @RPC
    135. function ApplyGlobalChatText (name : String, msg : String) {
    136.     var entry : ChatEntry = new ChatEntry ();
    137.     entry.playerName = name;
    138.     entry.playerText = msg;
    139.    
    140.     chatEntries.Add (entry);
    141.    
    142.     if (chatEntries.length > chatEntriesLength)
    143.         chatEntries.Shift ();
    144.    
    145.     scrollPosition.y = Mathf.Infinity;
    146.     inputField = "";
    147. }
    148.  
    149. function AddGameChatMessage (str : String) {
    150.     ApplyGlobalChatText (" - ", str);
    151.     if (Network.connections.length > 0)
    152.         NetworkView.RPC ("ApplyGlobalChatText", RPCMode.Others, " - ", str);
    153. }
    154.  
    155. @script RequireComponent (NetworkView)
    Code (csharp):
    1. // PlayerControl.js
    2.  
    3. var speed : float = 5;
    4.  
    5. function Start () {
    6.     if (!NetworkView.isMine)
    7.         enabled = false;
    8. }
    9.  
    10. function Update () {
    11.     if (NetworkView.isMine) {
    12.         var moveDir : Vector3;
    13.         moveDir = Vector3 (Input.GetAxis ("Horizontal"), 0, Input.GetAxis ("Vertical"));
    14.         transform.Translate (speed * moveDir * Time.deltaTime);
    15.     }
    16. }
    17.  
    18. function OnSerializeNetworkView (stream : BitStream, info : NetworkMessageInfo) {
    19.     if (stream.isWriting) {
    20.         var pos : Vector3 = transform.position;
    21.         stream.Serialize (pos);
    22.     } else {
    23.         var posRec = Vector3.zero;
    24.         stream.Serialize (posRec);
    25.         transform.position = posRec;
    26.     }
    27. }
     
  2. appels

    appels

    Joined:
    Jun 25, 2010
    Posts:
    2,687
    The error explains itself :
    An instance of type 'UnityEngine.NetworkView' is required to access non static member 'RPC
    Eddy.
     
  3. AkilaeTribe

    AkilaeTribe

    Joined:
    Jul 4, 2010
    Posts:
    1,149
    Well, the scripts are not on gameObject yet...and these errors forbid me to put them on gameObject. Isn't it nice ?

    Same goes for PlayerControl.
     
  4. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    then do as you are asked to instead of posting here.

    it doesn't help you. either you fix your compile errors or it won't work.

    Trivial, isn't it?
     
  5. AkilaeTribe

    AkilaeTribe

    Joined:
    Jul 4, 2010
    Posts:
    1,149
    But what it exactly wants me to do ? I don't neither ask stuff I am sure of the answer for fun, nor ask for vagues questions, you know. I am always being polite by greeting the readers in the beginning of a question, try to be the more precise, have a pretty good grammar, so you would be nice not to make fun of me, or believe I am asking crap.

    So, again, what does it exactly wants me to do, please ?
     
  6. AkilaeTribe

    AkilaeTribe

    Joined:
    Jul 4, 2010
    Posts:
    1,149
    I found the answer, it was irrelevant to what you both thought. Given the first post, you could also have found what was the problem, but your thoughts were already towards something wrong, so you missed it.

    Well, thanx you anyways appels.
     
  7. Eagle32

    Eagle32

    Joined:
    Jun 20, 2010
    Posts:
    89
    They weren't wrong, they were completely correct. The fact you think they were wrong clearly indicates a lack of understanding of the problem.

    Compiler errors always tell you what the problem is, you need to understand what they are saying so that you can see where you're going wrong. This is something all programmers have to learn, people don't sit and fix other peoples coding errors all day long.

    "An instance of type 'UnityEngine.NetworkView' is required to access non static member 'RPC'."

    This error usually occurs when you're trying to access a non static method as if it was static.

    When a method is non static you can't access it by ClassName.MethodName(). You have to access it by a specific class instance.

    In your case you were writing NetworkView.RPC(.....). Which is ClassName.MethodName(). What you actually wanted to do was use the networkView instance (which refers to an instance of the NetworkView class which is attached to a gameObject instance).

    So your typo of NetworkView instead of networkView meant you were refering to the class rather than an instance of the class.
     
  8. AkilaeTribe

    AkilaeTribe

    Joined:
    Jul 4, 2010
    Posts:
    1,149
    First, I'm not a programmer. I am medium in algorithhm, that's all.
    Second, I only have two months of experience on Unity.

    Facts that did not prevent me from trying to understand what was wrong for at least 30-60 full minutes (in the case you would think I am the type to give up easily).

    You think they were not wrong but how can you be sure ? Any on the answers gave me a real hint, or implied they were aware of the true nature ; the first answer just quoted an error message. I can read but I had never met an error like this so I have no clue and experience to use, to resolve that.
    The second message was just making fun of me, implying I was some lazy guy even not trying my hardest.
    But when you run out of ideas, what do you do ? Invent ideas, or ask for help ?

    Anyway...yes, it was NetworkView who had to be changed to networkView. I understood it after reading code example, and seeing the author using a small n. Same goes for networkView.isMine.

    Thanx you anyway, Eagle32, for your precise and correct answer, I will remember it in the future, for similar problems.
     
  9. RabidCabbage

    RabidCabbage

    Joined:
    Jul 28, 2014
    Posts:
    2
    Wow, I had to resurrect this 4 year old thread to point out how unnecessarily aggressive this response was, and nobody called you up on it?, really, not a single member of this community saw how severely uncalled for that response was? that's just sad. :(

    well AkilaeTribe, if you're still a member of this community that hates you, I'd like you to know, somebody got your back!
     
    Voraxx likes this.