- Home /
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:
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.NullReferenceException: Object reference not set to an instance of an object
Let me know if there is any other information i can present. Hopefully I can figure it out.
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.
Solved...i stupidly forgot to set the Photonview to observe the chat script >> Thanks a lot for your help bunny :)
Your answer
Follow this Question
Related Questions
Multiplayer photon problem 0 Answers
Photon Viking Demo error 3 Answers
Photon Networking: RPC call doesn't work over other clients 0 Answers
A node in a childnode? 1 Answer