Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by DRiNVAD3R · May 11, 2013 at 04:01 PM · errornetworkingchatcs0120

error CS0120: An object reference is required to access non-static member

I was making a chat for my game, after I finished scripting it in C# I got this error:

 Assets/chat/chat.cs(158,45): error CS0120: An object reference is required to access non-static member `UnityEngine.NetworkView.RPC(string, UnityEngine.RPCMode, params object[])'

I had also some other errors which I fixed but however I can't fix this one and truly need your help. I would appreciate any kind of help. Here's My Code:

 using UnityEngine;
 using System.Collections;
 
 public class chat : MonoBehaviour {
     
     bool usingChat = false;
     bool showChat = false;
     
     string inputField ="";
     
     Vector2 scrollposition;
     int width = 500;
     int height = 100;
     string playerName;
     float lastUnfocusTime = 0;
     Rect window;
     
     ArrayList playerList = new ArrayList();
     
     class PlayerNode
     {
         public string playerName;
         public NetworkPlayer player;
     }
     
     ArrayList chatEntires = new ArrayList();
     class chatEntry
     {
     public string name ="";
         public string text ="";
     }
     
     
     void Start () 
     {
     window = new Rect(Screen.width /2 - width /2, Screen.height - height + 5, width, height);
     playerName = PlayerPrefs.GetString("playerName", "");
         
         if (playerName=="") playerName = "RandomName" + Random.Range(1,999);
     }
     
     void OnConnectToServer ()
     {
     ShowChatWindow();
         networkView.RPC("TellServerOurName", RPCMode.Server, playerName);
         addGameChatMessage(playerName + " has just joined the chat!");
     }
     
     void OnServerIntialized()
     {
         PlayerNode newEntry = new PlayerNode();
         newEntry.playerName = playerName;
         newEntry.player = Network.player;
         playerList.Add(newEntry);
         addGameChatMessage(playerName + "has just joined the chat!");
     }
     
     PlayerNode GetPlayerNode(NetworkPlayer netPlay)
     {
         foreach (PlayerNode entry in playerList)
         {
         if(entry.player == netPlay)
                 return entry;
         }
         Debug.LogError("GetPlayerNode: Requested a playernode of non-existing player!");
         return null;
     }
     
     void OnPlayerDisconnected(NetworkPlayer netPlayer)
     {
         addGameChatMessage("A Player has disconnected");
         playerList.Remove(GetPlayerNode(netPlayer));
     }
         void OnDisconnectedFromServer()
     {
        CloseChatWindow();
     }
     
     [RPC]
     void TellServerOurName(string name, NetworkMessageInfo info)
     {
         PlayerNode newEntry = new PlayerNode();
         newEntry.playerName = playerName;
         newEntry.player = Network.player;
         playerList.Add(newEntry);
         addGameChatMessage(playerName + " has just joined the chat!");
     }
     void CloseChatWindow()
     {
         showChat = false;
         inputField = "";
         chatEntires = new ArrayList();
     }
     void ShowChatWindow()
     {
         showChat = false;
         inputField = "";
         chatEntires = new ArrayList();
     }
     
     void OnGUI()
     {
         
         if(!showChat) return;
         if (Event.current.type == EventType.keyDown && Event.current.character == '\n' & inputField.Length <= 0 )
         {
             if(lastUnfocusTime + .25f < Time.time)
             {
                 usingChat = true;
                 GUI.FocusWindow(5);
                 GUI.FocusControl("Chat input field");
             }
         }
         
         window = GUI.Window(5, window, GlobalChatWindow, "");
     }
     void GlobalChatWindow(int id)
     {
         GUILayout.BeginVertical();
         GUILayout.Space(10);
         GUILayout.EndVertical();
         
         scrollposition = GUILayout.BeginScrollView(scrollposition);
         foreach (chatEntry entry in chatEntires)
         {
             GUILayout.BeginHorizontal();
             if(entry.name == " - ")
                 GUILayout.Label(entry.name + entry.text);
             else
                 GUILayout.Label(entry.name + ": " + entry.text);
             
             GUILayout.EndHorizontal();
             GUILayout.Space(2);
         }
         
         GUILayout.EndScrollView();
         if(Event.current.type == EventType.keyDown && Event.current.character == '\n' & inputField.Length <= 0)
         {
             HitEnter(inputField);
             GUI.SetNextControlName("Chat input field");
             inputField = GUILayout.TextField(inputField);
             
             if (Input.GetKeyDown("mouse 0"))
             {
                 if (usingChat)
                 {
                     usingChat = false;
                     GUI.UnfocusWindow();
                     lastUnfocusTime = Time.time;
                 }
             }
 
         }
     }
     void HitEnter(string msg)
             {
                 msg = msg.Replace('\n', ' ') ;
                 NetworkView.RPC("ApplyGlobalChatText", RPCMode.All, playerName, msg);
             }
 [RPC]
     void ApplyGlobalChatText(string name, string msg)
     {
         chatEntry entry = new chatEntry();
         entry.name = name;
         entry.text = msg;
         
         chatEntires.Add(entry);
         
         if (chatEntires.Count > 4)
             chatEntires.RemoveAt(0);
         
         scrollposition.y = 1000000;
         inputField = "";
         
     }
     
     void addGameChatMessage(string str)
     {
         ApplyGlobalChatText(" - ", str);
         if(Network.connections.Length > 0)
             networkView.RPC("ApplyGlobalChatText", RPCMode.Others , " - ", str);
     }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by whebert · May 11, 2013 at 04:24 PM

Just change line 158 to:

 networkView.RPC("ApplyGlobalChatText", RPCMode.All, playerName, msg);

You had a capitol letter NetworkView.RPC which would be calling the class' method (which would need to be static to work), and not the RPC method on your object, which is what you want.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DRiNVAD3R · May 11, 2013 at 04:34 PM 0
Share

thanks it worked!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Network Chat not sending messages to other clients 1 Answer

Question regarding chat-input? 1 Answer

Need help converting js to c# 4 errors 1 Answer

The name `OnConnect' does not exist in the current context 1 Answer

UnityWebRequest.POST calls to an HTTPS url throws "Unknown error" on specific device 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges