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
1
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
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 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

· Add your reply
  • Sort: 
avatar image
0
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
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 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 :)

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

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

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

photon networking and unity networking 1 Answer


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