Passing variable to other script after button press to control which UI is displayed

I’m new at scripting so please let me know if I’m aproaching this completely wrong. I am having trouble passing a logged in state which is, for now, just triggered by pressing a gui button.

I have 3 scenes:
EntryScreen
Main (which is like a lobby)
Room1

EntryScreen just displays a button that says Enter.
When you press enter, it loads the Main scene.

Main has 2 gui buttons. One says Main (which loads the level Main) and the other says Room1 (which loads Room1).

I have an object in the EntryScreen called UIManager, with 3 scripts attached.

  1. UIEntry.js - this displays the UI for the entry screen.

  2. MainUI.js - this displays the UI for the game once you leave the EntryScreen scene.

  3. LoggedInUIControl.js - this one controls which of UI scripts are activated based on whether or not you are logged in. For now I just want to set the loggedIn state to true if you press the Enter button on the Entry Screen.

In the inspector, I have all 3 scripts enabled, but I have the LoggedInUIControl script turn off the other scripts on awake and if the button has not been pressed on the Entry screen, this script then enables the EntryUI script.

I have the entry script change the value of loggedIn to true, but I can’t get that value to be read by the loggedInUIControl script. It is remaining as false.

I can see in the inspector that the value of loggedIn is true for the UIEntry script.

Any help on this would be great. I just need to get the value of loggedIn in the LoggedInUIControl to equal the state of loggedIn from the UIEntry script.

Below are the scripts:

loggedInUIControl.js

#pragma strict

var loggedInFromEntry : UIEntry;
var loggedIn : boolean = false;


function Awake ()
{
	GetComponent(MainUI).enabled = false;
	GetComponent(UIEntry).enabled = false;

	DontDestroyOnLoad(this);
}

function Start () 
{
	loggedInFromEntry = GetComponent(UIEntry);
	loggedIn = loggedInFromEntry.loggedIn;

	if (loggedIn)
	{
		GetComponent(MainUI).enabled = true;
	}
	else
	{
		GetComponent(UIEntry).enabled = true;
	}
}

UIEntry.js

#pragma strict

var loggedIn : boolean = false;


function OnGUI ()
{
				
	if (GUI.Button(Rect(Screen.width/2 - 25, Screen.height/2 - 25, 50, 50), "Enter"))
	{
		loggedIn = true;
		Application.LoadLevel("Main");
	}
	
}

MainUI.js

#pragma strict


function OnGUI ()
{
			
	if (GUI.Button(Rect(Screen.width/2 - 25, Screen.height - 60, 50, 50), "Main"))
	{
		Application.LoadLevel("Main");
	}
	
	if (GUI.Button(Rect(Screen.width/2 + 50, Screen.height - 60, 100, 50), "Room1"))
	{
		Application.LoadLevel("Room1");
	}
	

}

If this is just for a single user Game then it’s probably a candidate for a static variable, just set in your logged in control script:

static var loggedIn : boolean = false;

My JS is poor so that might not be the right format for the command. Be very careful with statics though. You could also store the value in a script attached to the player as long as the player is don’t destroy on load.

So let’s sum it up, you have those three scripts and the problem is that though you think you linked two variables, only one is changing.

Well fact is, you are not linking them, you are just assigning the value of one to the other once and then they are separate.

So loggedIn in UIEntry is true or false but the value of loggedIn in loggedInUIControl remains false at all time depsite that line:

 loggedIn = loggedInFromEntry.loggedIn;

So I guess this is your issue.

Now the line above just took the right value and stored it in the left value. But changing the right value will not link the left value, they are value type.

What you need is that each time you update one, you also update the other, or instead of having two separate vairables, just read one from the other scripts.

Case 1:

UIEntry.js

 private var loggedIn:boolean =false;
 private function LoggedIn (value:boolean)
 {
       loggedIn = value;
       GetComponent(loggedInUIControl).loggedIn = loggedIn;
 }
 public function GetLoggedIn:boolean()
 {
     return loggedIn;
 }

Second case, loggedInUIControl does not have any variable and simply reach the value in UIEntry when needed as shown in the getter method above.