I was trying to figure out how to best edit variables of a game object during run time. I was think like a GUI that will stop the “main” game and allow for users to write in information about the game object or change variables in the game object like a in game edit mode. But I have not the foggiest idea about the best way to go around doing that.
For example each game object has a variable id and name When i hit the right mouse click when im looking at it I want to open like a gui that shows the variables and like a text box to edit/reenter/enter the information that is attached to the object.
I got how to look at the object and interact but Im a bit stumped on how to edit the values of the varialbes
Thanks for any help with this
Either make the variables public or (if you are using C#) use public properties so they can be accessed from outside objects.
Then pass the object you are interacting with as a parameter to your GUI script and do something like:
bool m_showGUI;
MyObject m_theObject;
string m_theNewName;
//enable the GUI
public void ShowGUI(MyObject p_theObject)
{
m_theObject = p_theObject;
m_showGUI = true;
}
//render the GUI
void OnGUI()
{
if(m_showGUI)
{
// draw gui stuff
m_theNewName = GUI.TextField(new Rect(10, 10, 200, 20), m_theNewName, 25);
if (GUI.Button (new Rect (10,30,150,100), "Finish"))
FinishedEditing();
}
}
// assign the name and close the GUI
void FinishedEditing()
{
m_theObject.Name = m_theNewName;
m_showGUI = false;
// continue running the game and whatever else you need to do here
}
First of all thanks for all the input,
Well Ive been working at this all night and still a bit stuck.
What happening is that when I right click it gives me a null reference basically it does not know if there an object there or not if I get the jist of things.
So this is my set up I have this class/scripts that basically holds information on a object and is attached to a cube or what not in a scene. Then I have my code that displays the information to the player.
This is the code for the data holder script:
using UnityEngine;
using System.Collections;
public class GeoData : MonoBehaviour {
public int id;
public string geoId;
public int height;
public string buildingType;
public string condition;
public string hwt;
}
And this is the code for the “gun” that shots a raycast at an object to get it infromation When it gets to the void EditGUI thats where it throws the object exception
NullReferenceException: Object reference not set to an instance of an object
UnityEngine.GUIUtility.GetControlID
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 showEditGui;
GameObject editObject;
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;
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;
lookingAtObject = true;
//destroy an object your looking at
if(Input.GetButtonDown ("Fire1"))
{
Destroy(hitInfo.collider.gameObject);
}
//edit object attributes
if(Input.GetButtonDown("Fire2"))
{
ShowEditGUI();
}
}
//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);
}
}
//enable the editorGUI
public void ShowEditGUI()
{
showEditGui = true;
Debug.Log("show gui: " + showEditGui);
if(showEditGui)
{
EditGUI();
}
}
void EditGUI()
{
// draw gui stuff
Debug.Log("SuperGus");
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;
showEditGui = false;
// continue running the game and whatever else you need to do here
}
}