Unity 3D 4 Networking issues - c#

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){

Related

How change GUI.Button/s color at runtime?

I'm trying to archive two things :
To be able to change for each individual button he own color in the inspector in runtime.
To make a color switch for example when clicking the "LOOP" button change the button color from his current color to another color like a switch between current color and blue. so i know that is loop is true or if loop pressed once make it blue if pressed again switch back to the original color/another color.
This is how the buttons looks like before using the GUI.backgroundColor in the original :
Before using the GUI.backgroundColor in the script :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GUIExamples : MonoBehaviour
{
public Texture btnTexture;
public Color color;
private void OnGUI()
{
if (!btnTexture)
{
Debug.LogError("Please assign a texture on the inspector");
return;
}
GUI.backgroundColor = color;
if (GUI.Button(new Rect(10, 10, 170, 30), "LOOP"))
Debug.Log("Clicked the button with an image");
GUI.backgroundColor = color;
if (GUI.Button(new Rect(10, 50, 170, 30), "CHANGE DIRECTION"))
Debug.Log("Clicked the button with text");
GUI.backgroundColor = color;
if (GUI.Button(new Rect(10, 90, 170, 30), "PING PONG"))
Debug.Log("Clicked the button with text");
GUI.backgroundColor = color;
if (GUI.Button(new Rect(10, 130, 170, 30), "STOP"))
Debug.Log("Clicked the button with text");
GUI.backgroundColor = color;
if (GUI.Button(new Rect(10, 170, 170, 30), "PAUSE"))
Debug.Log("Clicked the button with text");
}
}
Update :
This is what i tried but when i'm changing the color in the inspector in runtime it's not changing the box color :
It will change the color only if i set the color at this line like this :
currentStyle.normal.background = MakeTex( 2, 2, new Color( 0f, 1f, 0f, 0.5f ) );
But i want to change the color in runtime in real time in the inspector using the color variable.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GUIExamples : MonoBehaviour
{
public Texture btnTexture;
public Color color;
private GUIStyle currentStyle = null;
private Color oldColor;
private void Start()
{
oldColor = color;
}
private void OnGUI()
{
if (!btnTexture)
{
Debug.LogError("Please assign a texture on the inspector");
return;
}
if (GUI.Button(new Rect(10, 10, 170, 30), "LOOP"))
Debug.Log("Clicked the button with an image");
if (GUI.Button(new Rect(10, 50, 170, 30), "CHANGE DIRECTION"))
Debug.Log("Clicked the button with text");
if (GUI.Button(new Rect(10, 90, 170, 30), "PING PONG"))
Debug.Log("Clicked the button with text");
if (GUI.Button(new Rect(10, 130, 170, 30), "STOP"))
Debug.Log("Clicked the button with text");
InitStyles();
GUI.Box(new Rect(10, 170, 170, 30), "PAUSE", currentStyle);
if (GUI.Button(new Rect(10, 170, 170, 30), "PAUSE"))
Debug.Log("Clicked the button with text");
}
private void InitStyles()
{
if(oldColor != color)
{
currentStyle = null;
}
if (currentStyle == null)
{
currentStyle = new GUIStyle(GUI.skin.box);
currentStyle.normal.background = MakeTex(2, 2, new Color(color.r, color.g, color.b, color.a));
oldColor = color;
}
}
private Texture2D MakeTex(int width, int height, Color col)
{
Color[] pix = new Color[width * height];
for (int i = 0; i < pix.Length; ++i)
{
pix[i] = col;
}
Texture2D result = new Texture2D(width, height);
result.SetPixels(pix);
result.Apply();
return result;
}
}

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

Error saying missing function

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.

How do I remove a GUI object(s) in unity C#?

I have a controllable character that when you fall off map, a GUI will be created with a respawn button
void OnGUI() {
if(died==true){
GUI.Label(new Rect(daX, daY, 200, 40), "You died!!");
GUI.Label(new Rect(daX, daY-40, 200, 40), whyDie);
if (GUI.Button(new Rect(daX-75, daY+50, 150, 30), "Respawn!!!")){
transform.position = new Vector3(-5,20,5);
}
if (GUI.Button(new Rect(daX-75, daY+100, 150, 30), "Screw This!!!")){
Debug.Log("Back to menu");
}
}
}
The respawn and stuff works, is just that I don't know how to get rid of the GUI, so the respawn button stays there after you respawned. Does anyone know how to remove GUI objects? Thanks in advance
*EDIT: died, daX, daY, whyDie are variables I created and are valid
Just set your died variable back to false, and the GUI calls wont be made.
void OnGUI()
{
if(died)
{
GUI.Label(new Rect(daX, daY, 200, 40), "You died!!");
GUI.Label(new Rect(daX, daY-40, 200, 40), whyDie);
if (GUI.Button(new Rect(daX-75, daY+50, 150, 30), "Respawn!!!"))
{
transform.position = new Vector3(-5,20,5);
died = false; //Stop drawing died interface
}
if (GUI.Button(new Rect(daX-75, daY+100, 150, 30), "Screw This!!!"))
{
Debug.Log("Back to menu");
Application.LoadLevel(0); //Load your first scene, where the menu is.
}
}
}
Alternatively, you could disable the component/gameobject. That way your whole OnGUI method wont be called anymore.

XNA C# Creating a Heads Up Display

I was wondering how to approach creating a HUD.
I currently have health, mana, and experience bar drawn to the screen at set coordinates. Downside of this is that when the camera pans the bars stay at their set coordinates, I want them to adjust to the viewport or not be influenced by positions but just simply drawn to the screen.
Edit
I managed to get the HUD to adjust using the camera's x and y coordinates.
I've created a separate class for drawing the HUD, but now they don't adjust.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using GameOne.Components;
namespace GameOne.GameScreens
{
public class HUD : BaseGameState
{
Player player;
Texture2D HealthBar;
Texture2D HealthBarPositive;
Texture2D HealthBarNegative;
Texture2D ManaBar;
Texture2D ManaBarPositive;
Texture2D ManaBarNegative;
Texture2D ExpBar;
Texture2D ExpBarPositive;
int CurrentHealth = 100;
int CurrentMana = 45;
int CurrentExp = 0;
public HUD(Game game, GameStateManager manager)
: base(game, manager)
{
player = new Player(game);
}
public void LoadContent()
{
base.LoadContent();
HealthBar = Game.Content.Load<Texture2D>(#"GUI\healthBar");
HealthBarPositive = Game.Content.Load<Texture2D>(#"GUI\healthBarPositive");
HealthBarNegative = Game.Content.Load<Texture2D>(#"GUI\healthBarNegative");
ManaBar = Game.Content.Load<Texture2D>(#"GUI\manaBar");
ManaBarPositive = Game.Content.Load<Texture2D>(#"GUI\manaBarPositive");
ManaBarNegative = Game.Content.Load<Texture2D>(#"GUI\manaBarNegative");
ExpBar = Game.Content.Load<Texture2D>(#"GUI\expBar");
ExpBarPositive = Game.Content.Load<Texture2D>(#"GUI\expBarPositive");
}
public void Update(GameTime gameTime)
{
if (InputHandler.KeyDown(Keys.F1))
{
CurrentHealth += 1;
}
if (InputHandler.KeyDown(Keys.F2))
{
CurrentHealth -= 1;
}
if (InputHandler.KeyDown(Keys.F3))
{
CurrentMana += 1;
}
if (InputHandler.KeyDown(Keys.F4))
{
CurrentMana -= 1;
}
if (InputHandler.KeyDown(Keys.F5))
{
CurrentExp += 1;
}
if (InputHandler.KeyDown(Keys.F6))
{
CurrentExp -= 1;
}
CurrentHealth = (int)MathHelper.Clamp(CurrentHealth, 0, 100);
CurrentMana = (int)MathHelper.Clamp(CurrentMana, 0, 45);
CurrentExp = (int)MathHelper.Clamp(CurrentExp, 0, 500);
}
public void Draw(GameTime gameTime)
{
GameRef.SpriteBatch.Draw(
HealthBarNegative,
new Rectangle((int)player.Camera.Position.X + 150, (int)player.Camera.Position.Y + 630, 150, 15),
Color.White);
GameRef.SpriteBatch.Draw(
HealthBarPositive,
new Rectangle((int)player.Camera.Position.X + 150, (int)player.Camera.Position.Y + 630, 150 * (int)CurrentHealth / 100, 15),
Color.White);
GameRef.SpriteBatch.Draw(
HealthBar,
new Rectangle((int)player.Camera.Position.X + 150, (int)player.Camera.Position.Y + 630, 150, 15),
Color.White);
GameRef.SpriteBatch.Draw(
ManaBarNegative,
new Rectangle((int)player.Camera.Position.X + 150, (int)player.Camera.Position.Y + 650, 150, 15),
Color.White);
GameRef.SpriteBatch.Draw(
ManaBarPositive,
new Rectangle((int)player.Camera.Position.X + 150, (int)player.Camera.Position.Y + 650, 150 * (int)CurrentMana / 45, 15),
Color.White);
GameRef.SpriteBatch.Draw(
ManaBar,
new Rectangle((int)player.Camera.Position.X + 150, (int)player.Camera.Position.Y + 650, 150, 15),
Color.White);
GameRef.SpriteBatch.Draw(
ExpBarPositive,
new Rectangle((int)player.Camera.Position.X + 10, (int)player.Camera.Position.Y + 680, 1260 * (int)CurrentExp / 500, 15),
Color.White);
GameRef.SpriteBatch.Draw(
ExpBar,
new Rectangle((int)player.Camera.Position.X + 10, (int)player.Camera.Position.Y + 680, 1260, 15),
Color.White);
}
}
}
When you are drawing your Health bars, is it inside a Spritebatch.Begin( ... ) where you specify your camera matrix?
If you draw it in its own Spritebatch.Begin, without the camera, the position of the health bars will stay relative to the screen.
When you extend you class with DrawableGameComponent, you can use the property DrawOrder to set it topmost.
The class will be a bit different, you have to overload Update, draw and LoadContent, but it will be all the same.
This is only usefull if you use a HUD with SpriteBatch.

Categories