- Home /
If statement is not working properly
So this is what I am trying to do. When you click the left, right or middle mouse button I want to do one of 3 things. 1 display the information off the gameobject from a script. 2 destroy the gameObject, 3 edit the information that is attached to the gameObject. So the first two are working flawlessly. but the 3rd on is not working at all it wont even display the debug log that I put in it. I am completly lost why this is not working. Both functions are set up the same and they should behave the same way but it does not. Any help with this would be appreciated. The script that is attached to the gameObject is just a bunch of strings and ints not methods just so I can assign values in the unity Gui editor and then adjust them during runtime with the script
using UnityEngine;
using System.Collections;
public class GunTest : MonoBehaviour
{
//the script called GeoData
//it is attached to a cube that holds variables for that specific cube
public GeoData geoData;
//variable for accessing the information of the objects
private string targetName = string.Empty;
private string targetId;
private string targetGeoId;
private string targetHeight;
private string targetBuildingType;
private string targetCondition;
private string targetHWT;
private bool lookingAtObject;
//variables for editing menu
bool editObject;
bool showEditGui;
string editName;
// the result object
RaycastHit hitInfo;
private void Update()
{
// get the center of the gun
var origin = this.transform.position;
// get the direction of the gun
var direction = this.transform.forward;
// get the distance to test for (how long can the gun shoot)
var distance = 20.0f;
// the result object
//RaycastHit hitInfo;
// check if the player is targeting anything
if( !Physics.Raycast( origin, direction, out hitInfo, distance ) )
{
// nothing targeted
this.targetName = string.Empty;
this.targetId = string.Empty;
this.targetGeoId = string.Empty;
this.targetHeight = string.Empty;
this.targetBuildingType = string.Empty;
this.targetCondition = string.Empty;
this.targetHWT = string.Empty;
lookingAtObject = false;
editObject = false;
return;
}
// store the name of the target
this.targetName = hitInfo.collider.name;
//get the variable from the script
//assign geoId to local variable
this.geoData = hitInfo.collider.GetComponent<GeoData>();
int tId = geoData.id;
//tId.ToString();
this.targetId = tId.ToString();
this.targetGeoId = geoData.geoId;
int tH = geoData.height;
this.targetHeight = tH.ToString();
this.targetBuildingType = geoData.buildingType;
this.targetCondition = geoData.condition;
this.targetHWT = geoData.hwt;
//destroy an object your looking at left click
if(Input.GetButtonDown ("Fire1"))
{
lookingAtObject = true;
}
//edit object attributes right click
if(Input.GetButtonDown("Fire2"))
{
editObject = true;
}
if(Input.GetButtonDown("Fire3"))
{
Destroy(hitInfo.collider.gameObject);
}
}
//this is to display information from gameObject
private void OnGUI()
{
// output the name of what is targeted
if(lookingAtObject == true)
{
GUI.Box(new Rect(0,0,Screen.width/2,Screen.height/2), "GeoView");
GUILayout.Label("" );
GUILayout.Label("ID: " + this.targetId);
GUILayout.Label("GeoID: " +this.targetGeoId);
GUILayout.Label("Height: " + this.targetHeight);
GUILayout.Label("Building Type: " + this.targetBuildingType);
GUILayout.Label("Building Condition: " + this.targetCondition);
GUILayout.Label("House of Worship: " +this.targetHWT);
}
}
private void EditGUI()
{
Debug.Log("In Edit Gui");
if(editObject == true)
{
// draw gui stuff
Debug.Log("in If statement");
GUI.Box(new Rect(0,0,Screen.width/2,Screen.height/2), "GeoEditor");
//this.targetGeoId = GUI.TextField(new Rect(10, 10, 200, 20), editName, 25);
if (GUI.Button (new Rect (10,30,150,100), "Finish"))
FinishedEditing();
}
}
// assign the name and close the GUI
void FinishedEditing()
{
this.targetGeoId = editName;
editObject = false;
// continue running the game and whatever else you need to do here
}
}
Answer by MarkFinn · Sep 27, 2012 at 05:15 PM
You have the boolean value editObject which is set on click and the method EditGUI(), but you never do anything with either of them.
The display data and destroy work just fine, but you never actually call the EditGUI method.
So, add
if(editObject) EditGUI();
to your OnGUI method, and you should be fine.
Beat me to it. You always want to make sure you are calling GUI-related functions inside OnGUI so that they can update correctly. Just adding my two-cents. Here's what I was gonna say:
From my understanding, EditGUI needs to be in the OnGUI function. There is no updating happening; therefore, nothing will display since EditGUI is not being called. As far as I know, EditGUI isn't a Unity function, it is your own function that needs to be passed into an update like OnGUI.
AWWW thank you $$anonymous$$arkFinn and ThunderAwesome that got it working and now i get it. I just though that my own functions would update along with the onGUI, that clears things up a lot. Thanks again