Error saying missing function - c#

Error saying 'MPPlayer' could not be found, however I think it's referenced.
private void Menu_Lobby()
{
ScrollLobby = GUILayout.BeginScrollView(ScrollLobby, GUILayout.MaxWidth("200"));
foreach (MPPlayer pl in MultiplayerManager.instance.PlayerList)
{
GUILayout.Box (pl.PlayerName);
}
GUILayout.EndScrollView();
}
Full page code:
using UnityEngine;
using System.Collections;
public class MenuManager : MonoBehaviour
{
public string CurrentMenu;
public string MatchName = "";
public string MatchPassword = "";
public int MatchMaxPlayers = 20;
private Vector2 ScrollLobby = Vector2.zero;
void Start()
{
CurrentMenu = "Main";
MatchName = "My Server " + Random.Range(0 , 100);
}
void OnGUI()
{
if (CurrentMenu == "Main")
Menu_Main();
if (CurrentMenu == "Lobby")
Menu_Lobby();
if (CurrentMenu == "Host")
Menu_HostGame();
}
public void NavigateTo(string nextmenu)
{
CurrentMenu = nextmenu;
}
private void Menu_Main()
{
if (GUI.Button(new Rect(10,10,200,50), "Create Game"))
{
NavigateTo ("Host");
}
GUI.Label(new Rect(220, 10, 130, 30), "Player Name");
MultiplayerManager.instance.PlayerName = GUI.TextField( new Rect(350, 10, 150, 30), MultiplayerManager.instance.PlayerName);
if (GUI.Button (new Rect(510,10,100,30), "Save"))
{
PlayerPrefs.SetString("PlayerName", MultiplayerManager.instance.PlayerName);
}
}
private void Menu_HostGame()
{
// Buttons Host Game
if (GUI.Button(new Rect(10,10,200,50), "Back"))
{
NavigateTo("Main");
}
if (GUI.Button(new Rect(10,60,200,50), "Start Server"))
{
MultiplayerManager.instance.StartServer(MatchName, MatchPassword, MatchMaxPlayers);
}
GUI.Label(new Rect(220, 10, 130, 30), "Match Name");
MatchName = GUI.TextField( new Rect(400, 10, 200, 30), MatchName);
GUI.Label(new Rect(220, 50, 130, 30), "Match Password");
MatchPassword = GUI.PasswordField( new Rect(400, 50, 200, 30), MatchPassword, '*');
GUI.Label(new Rect(220, 90, 130, 30), "Match Max Players");
GUI.Label(new Rect(400, 90, 200, 30), MatchMaxPlayers.ToString());
MatchMaxPlayers = Mathf.Clamp (MatchMaxPlayers, 6, 20); // Allows min of 6 players and a max of 20 players.
if (GUI.Button (new Rect(425,90,25,30), "+"))
MatchMaxPlayers += 2; // Adds 2 players to the number
if (GUI.Button (new Rect(450,90,25,30), "-"))
MatchMaxPlayers -= 2; // Takes 2 players from the number
}
private void Menu_Lobby()
{
ScrollLobby = GUILayout.BeginScrollView(ScrollLobby, GUILayout.MaxWidth("200"));
foreach (MPPlayer pl in MultiplayerManager.instance.PlayerList)
{
GUILayout.Box (pl.PlayerName);
}
GUILayout.EndScrollView();
}
void OnServerInitialized()
{
NavigateTo("Lobby");
}
void OnConnectedToServer()
{
NavigateTo("Lobby");
}
}

maybe this would help
http://unity3d.com/learn/tutorials/modules
especially this one
http://unity3d.com/learn/tutorials/modules/beginner/scripting/variable-scope-and-access-modifiers

Change foreach (MPPlayer pl in MultiplayerManager.instance.PlayerList) to foreach (var pl in MultiplayerManager.instance.PlayerList) and mouseover in VS the var part to find out what's the actual type name.

Related

Method is not working inside onGUI() method

The thing that I want to do is writing a method which can call in onGUI() method.
I wrote this method. However when I run the program , method did not show the effect.
private void ShowSubPartsOnClick(float x, float y, float widthLABEL, float heigth, HumanBodyPart bodyPart)
{
x = x + 14;
for(int i = 0; i < bodyPart.SubParts.Count; i++)
{
y = y + 14;
GUI.Label(new Rect(x+14,y,widthLABEL,heigth), bodyPart.SubParts[i].EnglishTitle);
if(GUI.Button(new Rect(x, y, 14, heigth),"+"))
{
ShowSubPartsOnClick(x, y, widthLABEL, heigth, bodyPart.SubParts[i]);
}
}
}
}
private void OnGUI()
{
GUI.Label(new Rect(text.transform.position.x+14, text.transform.position.y, text.rectTransform.sizeDelta.x, 14),bodyVisualizer.BodyData.Body.SubParts[0].EnglishTitle);
if(GUI.Button(new Rect(text.transform.position.x, text.transform.position.y, 14, 14), "+"))
{
ShowSubPartsOnClick(text.transform.position.x, text.transform.position.y, text.rectTransform.sizeDelta.x, 14, bodyVisualizer.BodyData.Body.SubParts[0]);
}
}
How can I fix this or what is the problem?
The challenge here is functions like GUI.Label and GUI.Button must be invoked directly from OnGUI to work:
From Unity Forums: "The only place you can draw/create GUI elements is by triggering them from inside an OnGUI function."
Given the recommendation there, one solution is to run an iterative depth first search via a while loop. See attached example.
That being said, I'd highly recommend using Unity Canvas instead of OnGUI. It's much more powerful and its programmatic logic is not constrained to a single function.
OnGUI Snippet:
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class HumanBodyPart
{
public string EnglishTitle;
public List<HumanBodyPart> SubParts;
public bool IsExpanded;
public int DrawDepth;
public HumanBodyPart(string title, HumanBodyPart[] subParts)
{
this.EnglishTitle = title;
this.SubParts = new List<HumanBodyPart>();
this.SubParts.AddRange(subParts);
this.IsExpanded = false;
this.DrawDepth = 0;
}
}
public class Script : MonoBehaviour
{
[SerializeField]
Text text;
HumanBodyPart mainBodyPart;
private void Start()
{
HumanBodyPart subSubSubBodyPart = new HumanBodyPart("SubSubSubBodyPart", new HumanBodyPart[] { });
HumanBodyPart subSubBodyPart1 = new HumanBodyPart("SubSubBodyPart1", new HumanBodyPart[] { subSubSubBodyPart });
HumanBodyPart subSubBodyPart2 = new HumanBodyPart("SubSubBodyPart2", new HumanBodyPart[] { });
HumanBodyPart subBodyPart = new HumanBodyPart("SubBodyPart", new HumanBodyPart[] { subSubBodyPart1, subSubBodyPart2});
mainBodyPart = new HumanBodyPart("BodyPart", new HumanBodyPart[] { subBodyPart });
UpdateDrawDepths(mainBodyPart);
}
private void UpdateDrawDepths(HumanBodyPart currentBodyPart, int currentDrawDepth=0)
{
currentBodyPart.DrawDepth = currentDrawDepth;
foreach (HumanBodyPart bodyPart in currentBodyPart.SubParts)
{
UpdateDrawDepths(bodyPart, currentDrawDepth + 1);
}
}
private void OnGUI()
{
float spacing = 30;
float x = text.transform.position.x + spacing;
float y = text.transform.position.y;
int drawDepth = 0;
List<HumanBodyPart> nextPartsToRender = new List<HumanBodyPart>(new HumanBodyPart[] { mainBodyPart });
while (nextPartsToRender.Count > 0)
{
HumanBodyPart currentPart = nextPartsToRender[0];
GUI.Label(new Rect(currentPart.DrawDepth * spacing + x, y, 200, 20), currentPart.EnglishTitle);
nextPartsToRender.RemoveAt(0);
if (GUI.Button(new Rect(x - spacing + currentPart.DrawDepth * spacing, y, 20, 20), "+"))
{
currentPart.IsExpanded = true;
}
if (currentPart.IsExpanded)
{
nextPartsToRender.InsertRange(0, currentPart.SubParts);
}
y += spacing;
}
}
}

OnGUI will not show up

OnGui won't show up when I start the game for some reason. I've attached the script to a gameobject:
public class username : MonoBehaviour {
private static string user = "";
void OnGui()
{
GUI.Label(new Rect((Screen.width / 2), (Screen.height / 2), 300, 300), "Username");
user = GUI.TextField(new Rect(-4, 0, 100, 100), user);
if (GUI.Button(new Rect(-4, 0, 200, 30), "Continue"))
{
WWWForm form = new WWWForm();
form.AddField("", user);
WWW w = new WWW("http://site/register.php", form);
StartCoroutine(register(w));
}
}
Thanks
OnGui() is case-sensitive and needs to be OnGUI().
http://docs.unity3d.com/ScriptReference/MonoBehaviour.OnGUI.html

syntax error in photon networking for unity

Only assignment, call, increment, decrement, and new object expressions can be used as a statement
I'm not sure of what this error is.
There are no other syntax errors in the code
using UnityEngine;
using System.Collections;
public class NetworkManager : MonoBehaviour {
// Use this for initialization
void Start () {
PhotonNetwork.ConnectUsingSettings("1.0");
}
private const string roomName = "RoomName";
private RoomInfo[] roomsList;
void OnGUI()
{
if (!PhotonNetwork.connected)
{
GUILayout.Label(PhotonNetwork.connectionStateDetailed.ToString());
}
else if (PhotonNetwork.room == null)
{
// Create Room
if (GUI.Button(new Rect(100, 100, 250, 100), "Start Server"))
PhotonNetwork.CreateRoom(roomName + Guid.NewGuid().ToString("N"), true, true, 5);
// Join Room
if (roomsList != null)
{
for (int i = 0; i < roomsList.Length; i++)
{
if (GUI.Button(new Rect(100, 250 + (110 * i), 250, 100), "Join " + roomsList[i].name))
PhotonNetwork.JoinRoom(roomsList[i].name);
}
}
}
}
void OnReceivedRoomListUpdate()
{
roomsList = PhotonNetwork.GetRoomList();
}
void OnJoinedRoom()
{
Debug.Log("Connected to Room");
}
}
It should be roomsList.GetLength(0) . See here
try
int i = 0;
foreach (RoomInfo game in PhotonNetwork.GetRoomList())
{
if (GUI.Button(new Rect(5, (40 * (i + 2)) + 10, Screen.width - 10, 40), "Join" + game.name, _skin.button))
{
PhotonNetwork.JoinRoom(game.name);
}
i++;
}

Unity 3D 4 Networking issues

using UnityEngine;
using System.Collections;
public class Network1 : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnGUI() {
Debug.Log ("OnGUI()");
if (NetworkPeerType == NetworkPeerType.Disconnected){
if (GUI.Button (new Rect(10, 30, 120, 20), "Join a game")){
Network.Connect("127.0.0.1", 25001);
}
if (GUI.Button (new Rect(10, 50, 120, 20), "Host a game")){
Network.InitializeServer(32, 25001, false);
}
else if (Network.peerType == NetworkPeerType.Client){
GUI.Label(new Rect(10, 10, 300, 20), "Status: Connected as a Client");
if (GUI.Button (new Rect(10, 30, 120, 20), "Leave lobby")){
Network.Disconnect(200);
}
}
}
}
}
That's my code. It throws this error:
Assets/Network1.cs(15,21): error CS0119: Expression denotes a type', where avariable', value' ormethod group' was expected
I've googled it for a while now and can't seem to get a relevant answer.
Any help is appreciated.
if (NetworkPeerType == NetworkPeerType.Disconnected){
this should probably be:
if (Network.peerType == NetworkPeerType.Disconnected){

C# - Use of unassigned local variable

I'm making an account system for Unity and this is my script, I get the error on lines 132, 134, 136 before the TextField and PasswordFields. I can't tell what's wrong with it. Any help would be greatly appreciated.
using UnityEngine;
using System.Collections;
public class menuManager : MonoBehaviour {
public string currentMenu;
public string Username;
public string Password;
private string userTre = "holybuttwipe";
private string passTre = "tre";
private string userDerek = "Prouda You";
private string passDerek = "derek";
private string userBodey = "cherrypepsi72";
private string passBodey = "bodey";
private string userCody = "Bear On The Moon";
private string passCody = "cody";
private bool tre = false;
private bool derek = false;
private bool bodey = false;
private bool cody = false;
void Start() {
currentMenu = "Main";
}
void Update() {
}
void OnGUI() {
if(currentMenu == "Main")
Menu_Main();
if(currentMenu == "Login")
Menu_Login();
if(currentMenu == "Game")
Menu_Game();
if(currentMenu == "Create")
Menu_Create();
GUI.Label(new Rect(100, 100, 100, 100), Username);
}
public void NavigateTo(string nextmenu) {
currentMenu = nextmenu;
}
public void Menu_Main() {
if(GUI.Button(new Rect(10, 10, 300, 50), "Login")) {
NavigateTo("Login");
}
if(GUI.Button(new Rect(10, 60, 300, 50), "Create Account")) {
NavigateTo("Create");
}
}
public void Menu_Login() {
GUI.Label(new Rect(10, 10, 100, 30), "Username");
Username = GUI.TextField(new Rect(110, 10, 100, 30), Username);
GUI.Label(new Rect(10, 50, 100, 30), "Username");
Password = GUI.PasswordField(new Rect(110, 50, 100, 30), Password, '*');
if(GUI.Button(new Rect(10, 85, 210, 30), "Login")) {
if(Username == userTre) {
if(Password == passTre) {
NavigateTo("Game");
tre = true;
}
else {
Username = "";
Password = "";
}
}
else if(Username == userDerek) {
if(Password == passDerek) {
NavigateTo("Game");
derek = true;
}
else {
Username = "";
Password = "";
}
}
else if(Username == userBodey) {
if(Password == passBodey) {
NavigateTo("Game");
bodey = true;
}
else {
Username = "";
Password = "";
}
}
else if(Username == userCody) {
if(Password == passCody) {
NavigateTo("Game");
cody = true;
}
else {
Username = "";
Password = "";
}
}
else {
Username = "";
Password = "";
}
}
}
public void Menu_Game() {
GUI.Label(new Rect(10, 10, 300, 30), "You are logged in as " + Username);
if(GUI.Button(new Rect(10, 50, 300, 50), "Logout")) {
NavigateTo("Main");
tre = false;
derek = false;
bodey = false;
cody = false;
}
}
public void Menu_Create() {
string newUsername;
string newPassword;
string conPassword;
GUI.Label(new Rect(10, 10, 100, 30), "Username");
newUsername = GUI.TextField(new Rect(100, 10, 100, 30), newUsername);
GUI.Label(new Rect(10, 50, 100, 30), "Password");
newPassword = GUI.PasswordField(new Rect(100, 50, 100, 30), newPassword, '*');
GUI.Label(new Rect(10, 90, 100, 30), "Confirm Password");
conPassword = GUI.PasswordField(new Rect(100, 90, 100, 30), conPassword, '*');
if(GUI.Button(new Rect(10, 130, 200, 30), "Create Account")) {
if(newPassword == conPassword && newUsername != userTre && newUsername != userDerek && newUsername != userBodey && newUsername != userCody) {
Username = newUsername;
Password = newPassword;
NavigateTo("Login");
}
else {
newUsername = "";
newPassword = "";
conPassword = "";
}
}
}
}
newUsername = GUI.TextField(new Rect(100, 10, 100, 30), newUsername);
newPassword = GUI.PasswordField(new Rect(100, 50, 100, 30), newPassword, '*');
conPassword = GUI.PasswordField(new Rect(100, 90, 100, 30), conPassword, '*');
Here you are using newUsername, newPassword and conPassword without applying any value to them. You need to assign a string value to them before passing them as parameters.
Alternatively you could declare them like so :
string newUsername = "";
string newPassword = "";
string conPassword = "";
I suspect your problem is the following:
newUsername = GUI.TextField(new Rect(100, 10, 100, 30), newUsername);
You are using newUsername as a parameter to a function before it has been assigned any value. Sure, it will have a value AFTERWARD but it cannot get that far.
You are, of course, doing this three times with different variables.

Categories