• 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
Question by m5k · Sep 06, 2012 at 08:56 PM · multiplayerphotonchatmmo

Unity/Photon chat system error.

Hello all, i have recently started to use Unity with photon to produce a simple multi-player game. I have so far got the client connecting perfectly to the cloud service however i cannot seem to get a simple chat system based on rpc calls to function correctly. Upon entering text into the textfield and submition, a null reference error is generated.

Below is a copy of the chat script.


 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class Chat : Photon.MonoBehaviour
 {
     public static Chat SP;
     public List<string> messages = new List<string>();
 
     private int chatHeight = (int)140;
     private Vector2 scrollPos = Vector2.zero;
     private string chatInput = "";
     private float lastUnfocusTime = 0;
 
     void Awake()
     {
         SP = this;
     }
         
     void OnGUI()
     {        
         GUI.SetNextControlName("");
 
         GUILayout.BeginArea(new Rect(0, Screen.height - chatHeight, Screen.width, chatHeight));
 
         scrollPos = GUILayout.BeginScrollView(scrollPos);
         GUI.color = Color.red;
         for (int i = messages.Count - 1; i >= 0; i--)
         {
             GUILayout.Label(messages[i]);
         }
         GUILayout.EndScrollView();
         GUI.color = Color.white;
 
         GUILayout.BeginHorizontal(); 
         GUI.SetNextControlName("ChatField");
         chatInput = GUILayout.TextField(chatInput, GUILayout.MinWidth(200));
         
         if (Event.current.type == EventType.keyDown && Event.current.character == '\n'){
             if (GUI.GetNameOfFocusedControl() == "ChatField")
             {                
                 SendChat(PhotonTargets.All);
                 lastUnfocusTime = Time.time;
                 GUI.FocusControl("");
                 GUI.UnfocusWindow();
             }
             else
             {
                 if (lastUnfocusTime < Time.time - 0.1f)
                 {
                     GUI.FocusControl("ChatField");
                 }
             }
         }
 
         if (GUILayout.Button("SEND", GUILayout.Height(17)))
             SendChat(PhotonTargets.All);
         GUILayout.FlexibleSpace();
         GUILayout.EndHorizontal();
         GUILayout.EndArea();
     }
 
     public static void AddMessage(string text)
     {
         SP.messages.Add(text);
         if (SP.messages.Count > 15)
             SP.messages.RemoveAt(0);
     }
 
     [RPC]
     void SendChatMessage(string text, PhotonMessageInfo info)
     {
         AddMessage("[" + info.sender + "] " + text);
     }
 
     void SendChat(PhotonTargets target)
     {
         if (chatInput != "")
         {
            photonView.RPC("SendChatMessage", target, chatInput);
            chatInput = "";
         }
     }
 
     void OnLeftRoom()
     {
         this.enabled = false;
     }
 
     void OnJoinedRoom()
     {
         this.enabled = true;
     }
     void OnCreatedRoom()
     {
         this.enabled = true;
     }
 }


 



the error generated is:

NullReferenceException: Object reference not set to an instance of an object

Chat.SendChat2 (PhotonTargets target) (at Assets/Networking/Chat.cs:86) Chat.OnGUI () (at Assets/Networking/Chat.cs:58). Any help or advice would be great, as i can't for the life of me fix the issue. This is a slimmer version of the photon chat demo code.
Comment
worla

People who like this

1 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 m5k · Sep 06, 2012 at 10:47 PM 0
Share

Let me know if there is any other information i can present. Hopefully I can figure it out.

1 Reply

  • Sort: 
avatar image
Best Answer

Answer by Bunny83 · Sep 06, 2012 at 11:05 PM

Well, i never really used Photon, but are you sure that you have a PhotonView component attached to the gameobject where this script is attached to?

"photonView" is a property which uses GetComponent on your Photon.MonoBehaviour. This is the only thing i can think of here. GetComponent will return null when there is no PhotonView component on your gameobject.

Comment

People who like this

0 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 m5k · Sep 07, 2012 at 11:34 AM 0
Share

Solved...i stupidly forgot to set the Photonview to observe the chat script >> Thanks a lot for your help bunny :)

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

9 People are following this question.

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

Related Questions

Unity3D Multiplayer with Photon 1 Answer

RPC called by OnPhotonPlayerConnected doesn't work 2 Answers

Unity photon network - game room 1 Answer

hovering usernames with photon 1 Answer

Host unity game with photon 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