- Home /
The name `OnConnect' does not exist in the current context
I got some code for automatically joining matchmaking from StackExchange and when I tested it, it said "The name `OnConnect' does not exist in the current context". (I changed from MonoBehaviour to NetworkBehaviour too.) Here it is:
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.Networking.Match;
using UnityEngine.Networking.Types;
using System.Collections;
using System.Collections.Generic;
public class GameLoader : NetworkBehaviour {
bool done;
// Use this for initialization
void Start () {
NetworkManager.singleton.StartMatchMaker();
NetworkManager.singleton.matchMaker.ListMatches(0, 20, "Match", false, 0, 1, OnMatchList);
Debug.Log("Searching");
}
public virtual void OnMatchList(bool success, string extendedInfo, List<MatchInfoSnapshot> matchList)
{
if (success)
{
if (matchList.Count != 0)
{
Debug.Log("Matches Found");
NetworkManager.singleton.matchMaker.JoinMatch(matchList[0].networkId, "", "", "", 0, 1, OnMatchJoined);
}
else
{
Debug.Log("No Matches Found");
Debug.Log("Creating Match");
NetworkManager.singleton.matchMaker.CreateMatch("Match", 2, true, "", "", "", 0, 1, OnMatchCreate);
}
}
else
{
Debug.Log("ERROR : Match Search Failure");
}
}
public virtual void OnMatchJoined(bool success, string extendedInfo, MatchInfo matchInfo)
{
if (success)
{
Debug.Log("Match Joined");
MatchInfo hostInfo = matchInfo;
NetworkManager.singleton.StartClient(hostInfo);
OnConnect();
}
else
{
Debug.Log("ERROR : Match Join Failure");
}
}
public virtual void OnMatchCreate(bool success, string extendedInfo, MatchInfo matchInfo)
{
if (success)
{
Debug.Log("Match Created");
MatchInfo hostInfo = matchInfo;
NetworkServer.Listen(hostInfo, 9000);
NetworkManager.singleton.StartHost(hostInfo);
OnConnect();
}
else
{
Debug.Log("ERROR : Match Create Failure");
}
}
}
It's "OnConnect();" That calls an error.
The function doesn't exist. Why do you expect it to exist? Perhaps it belongs to another object and you need something like OtherObject.OnConnect();
Exactly. I understand I might have confused you. But what I wan't is to figure out what it does to replicate it. I'm currently diving in source code trying to find an alternative for now (I don't feel like docs have a lot on Network$$anonymous$$anagers...).
I can say they don't have any method called OnConnect() neither does any other class you are using. If you got this code from somewhere it was either using something that is no longer supported or you are missing the OnConnect() method that they had in their script.
Answer by brockmcn · Jul 11, 2017 at 07:49 PM
You're trying to call a method with "OnConnect();" , but you don't have that method in your script.
I know that. I am wondering what that function would do, in speculation.
Your answer
Follow this Question
Related Questions
Unity networking tutorial? 6 Answers
Not calling OnServerAddPlayer() when client connect 0 Answers
Problems with Scene Change networking Unet 0 Answers
send data from client to local server 0 Answers
Validating User Actions on the Server 0 Answers