- Home /
The gui disappears when i let go
Ive made a script so when i click an object like an npc i can talk to it with different responses but when i go to touch the responses the GUI goes i would like it so when i let go, The GUI stays till i press the close button :). Any help would be greatly appreciated
var target : Transform;
function OnGUI () {
///making a varible i named it position it has to hav a lowercase first letter ive made it equal to a 2d position space by putting Vector2 and the (x,y) is the place im ref var position = Vector2(0,0);
/// mr cubies position is (x = 6.14779,y = 1)
var screenPosX : Vector3 = camera.WorldToScreenPoint (target.position);
print ("target is " + screenPosX.x + " pixels from the left");
for (var touch : Touch in Input.touches) {
if (touch.phase == TouchPhase.Stationary) {
var touchPositionXRounded = Mathf.Round(touch.position.x / 100);
var touchPositionYRounded = Mathf.Round(touch.position.y / 100);
// screen position of gameobject
var screenPosY : Vector3 = camera.WorldToScreenPoint (target.position);
print ("target is " + screenPosY.y + " pixels from the top");
var oSPX = Mathf.Round(screenPosX.x / 100);
var oSPY = Mathf.Round(screenPosY.y / 100);
/// Debug.Log(touch.position);
Debug.Log(touchPositionXRounded);
Debug.Log(touchPositionYRounded);
Debug.Log(oSPX);
Debug.Log(oSPY);
// if touch position = the touch position of game object display a gui box
if (touchPositionXRounded == oSPX){
if (touchPositionYRounded == oSPY) {
GUI.Box(Rect(10,10,100,90),"Hello");
if (GUI.Button(Rect(10,80,100,90),"Close")) {
touch == false;
}
// if you click the button it displays a gui text box over the old one
if (GUI.Button(Rect(120,10,150,40),"Hi")){
GUI.Box(Rect(10,10,100,90),"Hello");
}
if (GUI.Button(Rect(120,60,150,40),"Go away!")){
GUI.Box(Rect(10,10,100,90),"fine then moody :(");
}
}
}
}
}
}
EDIT i added a boolean but still does not work :(
#pragma strict
var show : boolean;
var show2 : boolean;
var target : Transform;
var screenPosX : Vector3 = camera.WorldToScreenPoint (target.position);
print ("target is " + screenPosX.x + " pixels from the left");
var screenPosY : Vector3 = camera.WorldToScreenPoint (target.position);
print ("target is " + screenPosY.y + " pixels from the top");
// screen position of gameobject
var oSPX = Mathf.Round(screenPosX.x / 100);
var oSPY = Mathf.Round(screenPosY.y / 100);
if(show2 === true);
{
}
function Update () {
for (var touch : Touch in Input.touches) {
if (touch.phase == TouchPhase.Began) {
show = true;
Debug.Log("niceballsTrue");
Debug.Log(show);
var touchPositionXRounded = Mathf.Round(touch.position.x / 100);
var touchPositionYRounded = Mathf.Round(touch.position.y / 100);
}
else
{
show = false;
Debug.Log("False");
Debug.Log(show);
}
if(show === true)
{
Debug.Log("OMG IT WORKSish");
}
else if (touchPositionXRounded == oSPX)
{
if (touchPositionYRounded == oSPY) {
show2 = true;
}
Debug.Log("ITS ALIVE":)
}
else
{
Debug.Log("printed false :(");
}
}
}
Could you please edit your question and highlight your code part?
As Solid Snake said : for further help, please format your code. You can do this by highlighting all your code, then clicking the 10101 button at the top of the edit window.
Though, it may be easier just to delete the code, paste it in again from the source, then highlight all the code and press the 101010 button at the top of the edit window before posting.
you could remove the swearing too
Sorry alucardj, and thank you Both was struggling with that :)
Answer by joeyaaaaa · Feb 03, 2013 at 09:43 AM
i think you just need to
add something like
if (GUIbox == 1) { GUI.Box(Rect(10,10,100,90),"Hello"); if (GUI.Button(Rect(10,80,100,90),"Close")) { touch == false; } }
// if you click the button it displays a gui text box over the old one
if (GUI.Button(Rect(120,10,150,40),"Hi")){GUIbox = 0
GUI.Box(Rect(10,10,100,90),"Hello");
}
if (GUI.Button(Rect(120,60,150,40),"Go
away!")){ GUI.Box(Rect(10,10,100,90),"fine then moody :(");
after reading a few times tbh i am unsure of your question, but to make it stay say guibox = 1 lol
Yea I've sorted it now thx i set every thing in a update function so the gui was only staying for the frame it was clicked :/ I can't remember it all but the problem was fixed simply thx for answering this I put it up ages ago. The unity community is constantly surprising me! Sorry that it was unclear I am only thirteen everything I learnt was from the Internet. Thank you for answering this question after so long :D
no worries, and i misread alot of posts, your post was clear lol and i didnt realize it was an old post, it was high on the list ^^ yw. good luck!
Answer by Peterw301 · Feb 04, 2013 at 09:37 PM
My code is very confusing basically I didn't know about OnMouseDown() so my version of it was to get the position of the touch then divide it by 100 so it was less accurate to confiscate for big hands. So when you clicked a GUI box came up and you could put responses.
Your answer
Follow this Question
Related Questions
Create GUIText from Javascript 3 Answers
How to make GUI Text appear after a certain amount of time 2 Answers
touch 3d object open gui 1 Answer
iPhone GUI Text asks for edit on touch 0 Answers