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.
Related
I have a Xamarin C# IOS app I wrote, using a uitableviewsource to display an unordered list. Everything looks fine until I scroll up, then the text goes up into the header and when I scroll back down it disappears.
The code for the header is as follows
public override UIView GetViewForHeader(UITableView tableView, nint section)
{
UIView view = new UIView(new Rectangle(0, 65, 320, 90));
UILabel label = new UILabel
{
Opaque = true,
TextColor = UIColor.Black, //.FromRGB(190, 0, 0);
Font = UIFont.FromName("Helvetica-Bold", 16f),
Frame = new RectangleF(12, 70, 375, 20),
Text = "View Mileage",
TextAlignment = UITextAlignment.Center
};
view.AddSubview(label);
UIButton buttonRect = new UIButton
{
Opaque = true,
BackgroundColor = UIColor.FromRGB(191, 187, 189),
Font = UIFont.FromName("Helvetica-Bold", 16f),
Frame = new CGRect(145, 19, 100, 40)
};
buttonRect.SetTitle("Main Menu", UIControlState.Normal);
buttonRect.SetTitleColor(UIColor.Black, UIControlState.Normal);
buttonRect.HorizontalAlignment = UIControlContentHorizontalAlignment.Center;
buttonRect.VerticalAlignment = UIControlContentVerticalAlignment.Center;
buttonRect.TouchUpInside += delegate
{
UIStoryboard Storyboard = UIStoryboard.FromName("Main", null);
var webController = Storyboard.InstantiateViewController("MainMenuViewController") as MainMenuViewController;
Window = new UIWindow(UIScreen.MainScreen.Bounds);
this.Window.RootViewController = webController;
this.Window.MakeKeyAndVisible();
};
view.AddSubview(buttonRect);
UIButton buttonDate = new UIButton
{
Opaque = true,
BackgroundColor = UIColor.Brown,
Font = UIFont.FromName("Helvetica-Bold", 13f),
Frame = new CGRect(35, 105, 50, 25)
};
buttonDate.SetTitle("Date", UIControlState.Normal);
buttonDate.SetTitleColor(UIColor.White, UIControlState.Normal);
buttonDate.HorizontalAlignment = UIControlContentHorizontalAlignment.Center;
buttonDate.VerticalAlignment = UIControlContentVerticalAlignment.Center;
buttonDate.TouchUpInside += delegate
{
dataList.Sort((x, y) => x.Date.CompareTo(y.Date));
if (dateCounter % 2 != 0)
dataList.Reverse();
tableView.ReloadData();
dateCounter++;
};
view.AddSubview(buttonDate);
UIButton buttonMiles = new UIButton
{
Opaque = true,
BackgroundColor = UIColor.Brown,
Font = UIFont.FromName("Helvetica-Bold", 13f),
Frame = new CGRect(100, 105, 50, 25)
};
buttonMiles.SetTitle("Miles", UIControlState.Normal);
buttonMiles.SetTitleColor(UIColor.White, UIControlState.Normal);
buttonMiles.HorizontalAlignment = UIControlContentHorizontalAlignment.Center;
buttonMiles.VerticalAlignment = UIControlContentVerticalAlignment.Center;
buttonMiles.TouchUpInside += delegate
{
dataList.Sort((x, y) => x.Miles.CompareTo(y.Miles));
if (milesCounter % 2 == 0)
dataList.Reverse();
tableView.ReloadData();
milesCounter++;
};
view.AddSubview(buttonMiles);
UIButton buttonGas = new UIButton
{
Opaque = true,
BackgroundColor = UIColor.Brown,
Font = UIFont.FromName("Helvetica-Bold", 13f),
Frame = new CGRect(174, 105, 50, 25)
};
buttonGas.SetTitle("Gas", UIControlState.Normal);
buttonGas.SetTitleColor(UIColor.White, UIControlState.Normal);
buttonGas.HorizontalAlignment = UIControlContentHorizontalAlignment.Center;
buttonGas.VerticalAlignment = UIControlContentVerticalAlignment.Center;
buttonGas.TouchUpInside += delegate
{
dataList.Sort((x, y) => x.Gas.CompareTo(y.Gas));
if (gasCounter % 2 == 0)
dataList.Reverse();
tableView.ReloadData();
gasCounter++;
};
view.AddSubview(buttonGas);
UIButton buttonMpg = new UIButton
{
Opaque = true,
BackgroundColor = UIColor.Brown,
Font = UIFont.FromName("Helvetica-Bold", 13f),
Frame = new CGRect(244, 105, 50, 25)
};
buttonMpg.SetTitle("MPG", UIControlState.Normal);
buttonMpg.SetTitleColor(UIColor.White, UIControlState.Normal);
buttonMpg.HorizontalAlignment = UIControlContentHorizontalAlignment.Center;
buttonMpg.VerticalAlignment = UIControlContentVerticalAlignment.Center;
buttonMpg.TouchUpInside += delegate
{
dataList.Sort((x, y) => x.MPG.CompareTo(y.MPG));
if (mpgCounter % 2 == 0)
dataList.Reverse();
tableView.ReloadData();
mpgCounter++;
};
view.AddSubview(buttonMpg);
UIButton buttonCost = new UIButton
{
Opaque = true,
BackgroundColor = UIColor.Brown,
Font = UIFont.FromName("Helvetica-Bold", 13f),
Frame = new CGRect(314, 105, 50, 25)
};
buttonCost.SetTitle("Cost", UIControlState.Normal);
buttonCost.SetTitleColor(UIColor.White, UIControlState.Normal);
buttonCost.HorizontalAlignment = UIControlContentHorizontalAlignment.Center;
buttonCost.VerticalAlignment = UIControlContentVerticalAlignment.Center;
buttonCost.TouchUpInside += delegate
{
dataList.Sort((x, y) => x.Price.CompareTo(y.Price));
if (costCounter % 2 == 0)
dataList.Reverse();
tableView.ReloadData();
costCounter++;
};
view.AddSubview(buttonCost);
return view;
}
I am populating the table view using a UITableViewCell as follows;
public void SetData(string str0, string str1, string str2, string str3, int id, string str4)
{
lbC0.Text = str0;
lbC1.Text = str1;
lbC2.Text = str2;
lbC3.Text = str3;
lbcst.Text = str4;
lbId.Text = id.ToString();
}
public override void LayoutSubviews()
{
nfloat lbWidth = Bounds.Width / 4;
nfloat lbWidth2 = lbWidth - 35;
nfloat lbWidth3 = lbWidth2 + 70;
nfloat lbWidth4 = lbWidth3 + 70;
nfloat lbWidth5 = lbWidth4 + 65;
nfloat lbWidth6 = lbWidth5 + 75;
nfloat lbHeight = this.Bounds.Height;
if (lbC0 != null)
{
lbC0.Frame = new CGRect(2, 55, lbWidth, lbHeight);
lbC1.Frame = new CGRect(lbWidth2, 55, lbWidth, lbHeight);
lbC2.Frame = new CGRect(lbWidth3, 55, lbWidth, lbHeight);
lbC3.Frame = new CGRect(lbWidth4, 55, lbWidth, lbHeight);
lbcst.Frame = new CGRect(lbWidth5, 55, lbWidth, lbHeight);
}
}
Any help would be greatly appreciated.
Thanks
I spent weeks searching for an answer. What I finally found was I needed my subviews bounds within my TableViewCell’s bounds. This was all well and good, but I couldn’t figure out how to make it so. I tried everything I found, all to no avail. What finally ended up solving it for me was this. In my LayoutSubviews method, when setting the height of the subview, I subtracted 100 from Bounds.Height and that fixed it, it now scrolls up and down. How simple a fix... how hard to find
Here is the code I changed;
public override void LayoutSubviews()
{
nfloat lbWidth = Bounds.Width / 4;
nfloat lbWidth2 = lbWidth - 35;
nfloat lbWidth3 = lbWidth2 + 70;
nfloat lbWidth4 = lbWidth3 + 70;
nfloat lbWidth5 = lbWidth4 + 65;
nfloat lbWidth6 = lbWidth5 + 75;
-->nfloat lbHeight = this.Bounds.Height - 100;<--
if (lbC0 != null)
{
lbC0.Frame = new CGRect(2, 55, lbWidth, lbHeight);
lbC1.Frame = new CGRect(lbWidth2, 55, lbWidth, lbHeight);
lbC2.Frame = new CGRect(lbWidth3, 55, lbWidth, lbHeight);
lbC3.Frame = new CGRect(lbWidth4, 55, lbWidth, lbHeight);
lbcst.Frame = new CGRect(lbWidth5, 55, lbWidth, lbHeight);
}
}
I really need to shorten my code and turn massive IF operator blocks into LOOP (for, foreach).
When it is in IF blocks - code works.
When it is in FOR loop - code does not work sometimes.
Help would be nice.
IF blocks:
PictureBox tmp = new PictureBox();
tmp.Bounds = pbxDators.Bounds;
tmp.SetBounds(tmp.Location.X, tmp.Location.Y, 1, 15);
if (dt.Bounds.IntersectsWith(tmp.Bounds))
{
atskanotAudio(1);
bumbasStiprums = 3;
return true;
}
tmp.SetBounds(tmp.Location.X, tmp.Location.Y + 10, 1, 15);
if (dt.Bounds.IntersectsWith(tmp.Bounds))
{
atskanotAudio(1);
bumbasStiprums = 2;
return true;
}
tmp.SetBounds(tmp.Location.X, tmp.Location.Y + 10, 1, 15);
if (dt.Bounds.IntersectsWith(tmp.Bounds))
{
atskanotAudio(1);
bumbasStiprums = 1;
return true;
}
tmp.SetBounds(tmp.Location.X, tmp.Location.Y + 10, 1, 15);
if (dt.Bounds.IntersectsWith(tmp.Bounds))
{
atskanotAudio(1);
bumbasStiprums = 0;
return true;
}
tmp.SetBounds(tmp.Location.X, tmp.Location.Y + 10, 1, 15);
if (dt.Bounds.IntersectsWith(tmp.Bounds))
{
atskanotAudio(1);
bumbasStiprums = -1;
return true;
}
tmp.SetBounds(tmp.Location.X, tmp.Location.Y + 10, 1, 15);
if (dt.Bounds.IntersectsWith(tmp.Bounds))
{
atskanotAudio(1);
bumbasStiprums = -2;
return true;
}
tmp.SetBounds(tmp.Location.X, tmp.Location.Y + 10, 1, 15);
if (dt.Bounds.IntersectsWith(tmp.Bounds))
{
atskanotAudio(1);
bumbasStiprums = -3;
return true;
}
return false;
And the FOR loop I tried to write to replace that massive IFs:
for (int i = -3; i <= 3; i++)
{
PictureBox tmp = new PictureBox();
tmp.Bounds = pbxDators.Bounds;
int a = 10;
if (i == 3)
{
tmp.SetBounds(tmp.Location.X, tmp.Location.Y, 1, 15);
if (dt.Bounds.IntersectsWith(tmp.Bounds))
{
atskanotAudio(2);
bumbasStiprums = 2;
return true;
}
}
else
{
tmp.SetBounds(tmp.Location.X, tmp.Location.Y + a, 1, 15);
a = a + 10;
if (dt.Bounds.IntersectsWith(tmp.Bounds))
{
atskanotAudio(2);
bumbasStiprums = 2;
return true;
}
}
}
return false;
All that code is inside a public function which gets picturebox object as parameter and return bool value..
Why is my FOR loop not working in most cases? If the ball hits middle its sometimes OK but mostly the ball flies through the object bounds. No errors are generated.
This seems to be the code you need based on your first code:
PictureBox tmp = new PictureBox();
tmp.Bounds = pbxDators.Bounds;
tmp.SetBounds(tmp.Location.X, tmp.Location.Y, 1, 15);
for (var i = 3; i >= -3; i--)
{
if (dt.Bounds.IntersectsWith(tmp.Bounds))
{
atskanotAudio(1);
bumbasStiprums = i;
return true;
}
tmp.SetBounds(tmp.Location.X, tmp.Location.Y + 10, 1, 15);
}
return false;
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
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++;
}
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.