Saving player choices to a variable - Lerpz3D

Hi!

I am working on a GUI pop up where Lerpz is given a few choices. I want to store this in a variable somehow. How would I do with with the onGui() function?

Thanks!

Not completely sure what you’re asking here, but all of the interactive GUI control functions return a value indicating what happened with the control or what value was entered or selected. Presumably, you would just store this returned value in a variable.

If that doesn’t answer your question, maybe you could elaborate on it a bit.

Well, if we took a look at the following script (from the Start Menu of the 3D Lerpz Game), how is the onGUI function storing a variable here? It looks like if the player chooses, an action takes place right after versus the choice being stored in a variable. Thanks!

//@script ExecuteInEditMode()
var gSkin : GUISkin;
var backdrop : Texture2D;
private var isLoading = false;
function OnGUI()
{
if(gSkin)
GUI.skin = gSkin;
else
Debug.Log(“StartMenuGUI : GUI Skin object missing!”);
var backgroundStyle : GUIStyle = new GUIStyle();
backgroundStyle.normal.background = backdrop;
GUI.Label ( Rect( ( Screen.width - (Screen.height * 2)) * 0.75, 0, Screen.height * 2,
Screen.height), “”, backgroundStyle);
GUI.Label ( Rect( (Screen.width/2)-197, 50, 400, 100), “Lerpz Escapes”,
“mainMenuTitle”);
if (GUI.Button( Rect( (Screen.width/2)-70, Screen.height -160, 140, 70), “Play”))
{
isLoading = true;
Application.LoadLevel(“TheGame”);
}
var isWebPlayer = (Application.platform == RuntimePlatform.OSXWebPlayer ||
Application.platform == RuntimePlatform.WindowsWebPlayer);
if (!isWebPlayer)
{
if (GUI.Button( Rect( (Screen.width/2)-70, Screen.height - 80, 140, 70),
“Quit”)) Application.Quit();
}
if (isLoading)
{
GUI.Label ( Rect( (Screen.width/2)-110, (Screen.height / 2) - 60, 400, 70),
“Loading…”, “mainMenuTitle”);
}
}

if(GUI.Button(...)) // button 1
{
  yourVariable = someValue;
}
if(GUI.Button(...)) // button 2
{
  yourVariable = someOtherValue;
}

// ...

if(yourVariable == someValue)
{
  //do this
}
else if(yourVariable == someOtherValue)
{
  //do that
}
else
{
  //do something else
}

I was planning on adding the GUI after the OnTriggerEnter in the Pickup Script of the Lerpz 3D tutorial, does this make sense?

function OnTriggerEnter (col : Collider) {
	if(mover  mover.enabled) return;
	var playerStatus : ThirdPersonStatus = col.GetComponent(ThirdPersonStatus);
}

function OnGui ( ){
if …}

Thanks.

no. OnGUI() automatically gets called every frame, the position in the script doesn’t make any difference at all.
you should probably put the OnGUI() function in another script and enable/disable the whole script when you collide with something.

function OnGUI (){ 
if(inTrigger) 
if(GUI.Button (Rect (10,10,150,100), "Yeah!")) 

{ 
yeah = true; 
rand = Random.Range(0,2); 
if (rand == 0){ 
print("Yeah!"); 
} 
if (rand == 1){ 
print("Too bad!"); 
} 
} 
function OnTriggerEnter (col : Collider) { 
if(mover  mover.enabled) return; 
var playerStatus : ThirdPersonStatus = col.GetComponent(ThirdPersonStatus); 
inTrigger = true;

This however is not triggering the GUI Button to pop up for the player. I’m not sure why!
Thanks!

I don’t know if was just a re-typing mistake, but the code you’ve posted isn’t actually valid syntax (you didn’t have the curly braces matched correctly). I think what you wanted to do was this:-

function OnGUI (){ 
	if(inTrigger) 
		if(GUI.Button (Rect (10,10,150,100), "Yeah!")) { 
			yeah = true; 
			rand = Random.Range(0,2); 
			
			if (rand == 0){ 
				print("Yeah!"); 
			}
			 
			if (rand == 1){ 
				print("Too bad!"); 
			}
		} 
}

function OnTriggerEnter (col : Collider) { 
	if(mover  mover.enabled) return; 
	
	var playerStatus : ThirdPersonStatus = col.GetComponent(ThirdPersonStatus);
	inTrigger = true;
}

That’s strange. It seemed like I had the right # of {} in the right places, so why would spacing matter? Is there a good reference to browse through to pick up on proper “syntax”?

I used the spacing and line segmentation you had, and it worked, thank you!

I am now attempting to destroy the copper gameobject after yeah=true rand==0. Also, I need to be able to enable/disable the attack script. Not sure how to do this.

function OnGUI (){ 
   if(inTrigger) 
      if(GUI.Button (Rect (10,10,150,100), "Yeah!")) { 
         yeah = true; 
         rand = Random.Range(0,2); 
          
         if (rand == 0){ 
            print("Yeah!"); 
            [color=red]Destroy (Copper.EnemyPoliceGuy);[/color]
         } 
          
         if (rand == 1){ 
            print("Too bad!"); 
         } 
      } 
} 

function OnTriggerEnter (col : Collider) { 
   if(mover  mover.enabled) return; 
    
   var playerStatus : ThirdPersonStatus = col.GetComponent(ThirdPersonStatus); 
   inTrigger = true; 
}

[/code]

You can check for both conditions at once using the “and” operator:-

if (yeah  rand == 0) {
   ...
}

The script actually has a property called enabled that will disable script updates if you set it to false.

OK, so I looked into the enable property of the script, and followed this method:
http://forum.unity3d.com/viewtopic.php?t=30681

However, this was the following error I received when adapting it on the PIckup script referencing the EnemyDamage script through the enemystop variable that is declared:

var enemystop : EnemyDamage;
if (yeah == true  rand == 1)
{
enemystop.enabled = false;
}

NullReferenceException: Object reference not set to an instance of an object
Pickup.OnGUI () (at Assets/Scripts/Misc/Pickup.js:48)

You never assign any value to “enemystop”.

I did… I dragged in the value to the variable in the inspector. The value was the Copper gameobject

Can you post your whole code file then?

enum PickupType { Health = 0, FuelCell = 1 }
var pickupType = PickupType.FuelCell;
var amount = 1;
var sound : AudioClip;
var soundVolume : float = 2.0;
private var yeah = false; 
private var inTrigger = false;
private var used = false;
private var mover : DroppableMover;
private var rand : int;
var playerStatus : ThirdPersonStatus;

function Start ()
{
// do we exist in the level or are we instantiated by an enemy dying?
	mover = GetComponent(DroppableMover);
}

function ApplyPickup (playerStatus : ThirdPersonStatus)
{
// A switch...case statement may seem overkill for this, but it makes adding new pickup types trivial.
	switch (pickupType)
	{
		case PickupType.Health:
			playerStatus.AddHealth(amount);
			break;
		
		case PickupType.FuelCell:
			playerStatus.FoundItem(amount);
			break;
	}
	
	return true;
}
function OnTriggerEnter (col : Collider) {
	if(mover  mover.enabled) return;
		
	var playerStatus : ThirdPersonStatus = col.GetComponent(ThirdPersonStatus);
	inTrigger = true;
	if(yeah == true){
	//* Make sure we are running into a player
	//* prevent picking up the trigger twice, because destruction
	//  might be delayed until the animation has finished
	if (used || playerStatus == null)
		return;
	
	if (!ApplyPickup (playerStatus))
		return;

	used = true;
	
	// Play sound
	if (sound)
		AudioSource.PlayClipAtPoint(sound, transform.position, soundVolume);
		
	
	
	// If there is an animation attached.
	// Play it.
	if (animation  animation.clip)
	{
		animation.Play();
		Destroy(gameObject, animation.clip.length);
	}
	else
	{
		Destroy(gameObject);
	}
}	
}
function Reset ()
{
	if (collider == null)	
		gameObject.AddComponent(BoxCollider);
	collider.isTrigger = true;
}

@script RequireComponent(SphereCollider)
@script AddComponentMenu("Third Person Props/Pickup")
function OnGUI (){ 
if(inTrigger) 
if(GUI.Button (Rect (10,10,150,100), "Yeah!")) { 
yeah = true; 
rand = Random.Range(0,2); 

if (rand == 0){ 
print("Yeah!"); 
[color=red]Destroy (Copper.EnemyPoliceGuy);[/color] 
} 

if (rand == 1){ 
print("Too bad!"); 
} 
} 
} 

function OnTriggerEnter (col : Collider) { 
if(mover  mover.enabled) return; 

var playerStatus : ThirdPersonStatus = col.GetComponent(ThirdPersonStatus); 
inTrigger = true; 
}

I had originally put in the “Destroy(Copper.EnemyPoliceGuy)”, however, I basically just need to figure out how to disable the copper from attacking upon a set of true conditions (such as yeah rand == 0).

Thanks!

I can’t make heads or tails of that, especially since I’m not sure where to look anymore since you changed your code. Are you getting compile errors with that or just looking for us to debug the logic?

Also, in your last message, was the line “var enemystop : EnemyDamage;” located inside the method?

The error I am getting that I mentioned previously is:
NullReferenceException: Object reference not set to an instance of an object
Pickup.OnGUI () (at Assets/Scripts/Misc/Pickup.js:48)

this is when I declare the variable referencing the EnemyDamage script (per my previous post). I then get that error if when with, variable.enabled=false;

This is after I pull the Copper into the inspector

Where do you assign “Copper”? I only see you reference it, but never declare or assign it.

What’s the difference in putting in the gameobject in the inspector versus declaration or assigning it? I guess this is where I need help. Thank you!

var enemystop : EnemyDamage; is located along with all the other variables in the Pickup script. Not in the method itself.

I asked because I didn’t see the declaration in your script; did you only post part of it?