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 josifjoey · May 07, 2011 at 11:50 PM · networkingmultiplayerchat

Online Chat doesn't work

Hello all iv been in the making for a game called war season its an Online fps and just last night i was looking at a tutorial on how do make a basic muytiplayer the connection and character spwaning works fine its just the chat i finished the chat script but it has a bug and the script look fine can any help me if im missing somthing or somthing is wrong

this is the script

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 LastOnFocusedtime = 0;
 Rect window;

 ArrayList playerList = new ArrayList ();
 class PlayerNode
 {
     public string PlayerName;
     public NetworkPlayer player;
 }

 ArrayList chatEntries = new ArrayList();
 class ChatEntry
 {
     public string name = "";
     public string text = "";
 }

 // Use this for initialization
 void Start () 
 {
     window = new Rect(Screen.width /2 - width /2, Screen.height - height + 5, width, height);
     playerName = PlayerPrefs.SetString("Playername", "");

     if(playerName == "") playerName = "RandomName" + Random.Range(1, 999);
 }

 void OnConnectedToServer()
 {
     ShowChatWindow();
     networkView.RPC("TellServerOurName", RPCMode.Server, playerName);
     addGameChatMessage(playerName + " Have just joined the chat");
 }

 void OnServerInitialized()
 {
     showChatWindow();
     PlayerNode newEntry = new PlayerNode();
     newEntry.PlayerName = playerName;
     newEntry.player = Network.player;
     playerList.Add(newEntry);
     addGameChatMessage(playerName + " Have 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 disconected");
     playerList.Remove(GetPlayerNode(netplayer));
 }

 void OnDisconectedFromServer()
 {
     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 joined the Chat!");
 }

 void CloseChatWindow()
 {
     showChat = false;
     inputField = "";
     chatEntries = new ArrayList();
 }

 void ShowChatWindow()
 {
     showChat = true;
     inputField = "";
     chatEntries = new ArrayList();
 }

 void OnGUi()
 {
     if(!showChat) return;

     if(Event.current.type == EventType.keyDown && Event.current.character == '\n' & inputField.Length <=0)
     {
         if(LastOnFocusedtime + .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 chatEntries)
     {
         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();
             LastOnFocusedtime = Time.time;
         }
     }

     void HitEnter (string msg)
     {
         msg = msg.Replace('\n', ' ');
         networkView.RPC("ApplyGobalChatText", RPCMode.All, playerName, msg);
     }

     [RPC]
     void ApplyGlobalChatText(String name,String msg)
     {
         ChatEntry entry = new ChatEntry();
         entry.name = name;
         entry.text = msg;

         chatEntries.Add(entry);

         if(chatEntries.Count > 4)
             chatEntries.RemoveAt(0);

         ScrollPosition.y = 1000000;
         inputField = "";
         }

         void addGameChatMessage(string str)
         {
             ApplyGlobalChatText(" . ", str);
             if (Network.connections.Length > 0);
                 networkView.RPC("ApplyGlobalChatText", RPCMode.Others, " . ", str);
         }
 }

}

this is were its not working right it says im missing something in this line

void HitEnter (string msg)
        {
            msg = msg.Replace('\n', ' ');
            networkView.RPC("ApplyGobalChatText", RPCMode.All, playerName, msg);
        }

so if any one can help please do thanks!

Comment
Add comment · Show 1
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 Justin Warner · May 08, 2011 at 12:10 AM 1
Share

Can you give us the actual error too please? =)

1 Reply

· Add your reply
  • Sort: 
avatar image
4

Answer by superpig · May 08, 2011 at 12:24 AM

Typo, you fool! The string

"ApplyGobalChatText"

should be

"ApplyGlobalChatText"

You're missing an l.

Comment
Add comment · Show 3 · 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 Joshua · May 08, 2011 at 01:08 AM 0
Share

How the hell did you manage to spot that? I'm impressed xD

avatar image superpig ♦♦ · May 08, 2011 at 07:53 PM 1
Share

This is why good, clear, consistent, correct na$$anonymous$$g is important ;-)

avatar image josifjoey · May 11, 2011 at 01:24 AM 0
Share

Ahhh!! Dammit I still barely noticed it thanks allot man :)

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

No one has followed this question yet.

Related Questions

Unity networking tutorial? 6 Answers

How can I create a simple chat system for my multiplayer game? 1 Answer

CHAT Multiplayer Problem 1 Answer

Multiplayer text Chat, based on Command and ClientRpc not working. 0 Answers

Authoritative server basics Unity 5.2 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