Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Masurk · Dec 05, 2016 at 07:33 AM · networkingclient-servermessage

Unity messaging problem - cant send from client to server

Hi guys! I really cant handle a problem with sending messages. Every time when I try to send message i get "NullReferenceException: Object reference not set to an instance of an object Message+MasterClient.RegisterHost (System.String name) (at Assets/Message.cs:27) Message.Update () (at Assets/Message.cs:34)". Here is my code, please help if possible

Client: using UnityEngine; using UnityEngine.Networking;

 public class Message : NetworkBehaviour
 {
     class RegisterHostMessage : MessageBase
     {
         public string gameName;
         public string comment;
         public bool passwordProtected;
 
     }
 
     class MasterClient 
     {
         public NetworkClient client;
 
         public const short RegisterHotsMsgId = 888;
 
         public void RegisterHost(string name)
         {
             RegisterHostMessage msg = new RegisterHostMessage();
             msg.gameName = name;
             msg.comment = "test";
             msg.passwordProtected = false;
 
             client.Send(RegisterHotsMsgId, msg);
         }
     }
 
     void Update()
     {
             MasterClient mast = new MasterClient ();
             mast.RegisterHost ("works");
     }
 }

Server: using UnityEngine; using UnityEngine.Networking; using System.Collections;

public class ServerStart : NetworkBehaviour {

 void Start()
 {
     Debug.Log ("Registering server callbacks");

 }
 void Update()
 {
     NetworkServer.RegisterHandler(888, OnMessage);
 }
 void OnMessage(NetworkMessage netMsg)
 {
     Debug.Log ("Client get");
 }


}

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
0

Answer by cakeslice · Dec 05, 2016 at 09:47 PM

Sorry, there's a lot of problems here, you really should learn the programming basics before you try something like this.

In case you're wondering what's wrong:

  • Why is this not instantiated (what causes your error): public NetworkClient client;

  • Why are you doing this in the Update() instead of Start(): MasterClient mast = new MasterClient ();

  • Again, why are you doing this every frame: NetworkServer.RegisterHandler(888, OnMessage);

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 Masurk · Dec 06, 2016 at 05:28 AM 0
Share

Cackeslice, thank you for your answer! I am indeed new in coding, but I obviously know that Updade func is a frame func. The problem is that if I paste "NetworkServer.RegisterHandler" in start func literally nothing happens. I just tried an example: using UnityEngine; using UnityEngine.Networking; using System.Collections;

 public class ServerStart : NetworkBehaviour {
 
     void Start()
     {
         Debug.Log ("Registering server callbacks");
         NetworkServer.RegisterHandler($$anonymous$$sgType.Connect, OnConnected);
 
     }
     void Update()
     {
         
     }
     void OnConnected(Network$$anonymous$$essage net$$anonymous$$sg)
     {
         Debug.Log ("Client connected");
     }
 
 }

But it works only if I paste "NetworkServer.RegisterHandler" in update func, or else - just nothing, not even an error. Can you help me with this issue?

avatar image Masurk · Dec 06, 2016 at 06:04 AM 0
Share

If it is possible, could you please share an example of code?

avatar image cakeslice Masurk · Dec 06, 2016 at 11:22 AM 0
Share

https://docs.unity3d.com/$$anonymous$$anual/UNetClientServer.html

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to receive message from NetworkConnection 1 Answer

NullReference Exception FindLocalObject C# 1 Answer

Client with authority can't send information to server 0 Answers

How to watch in client applications what it is happening in the server one? 0 Answers

UNET Different size of spawned object on host and client 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