-1

I just want to make restart button with touch button via below code, but if statment does not work. Could you find reason?

function OnGUI() {
    if(Input.touchCount>0) {

        if (GUI.Button(Rect(Screen.width/1.12, 0, Screen.width/8, Screen.height/8),"restart")) {

            Application.LoadLevel(1);
        }
    }
}
user3071284
  • 6,955
  • 6
  • 43
  • 57
ugur albayrak
  • 35
  • 1
  • 9

2 Answers2

1

From what i see. The button wont even show up unless they already have at least a finger down. It isnt working doesnt tell me anything. Is the button showing up at all or does nothing happen when you attempt to hit it?

Im not sure if UGUI ifs work different on mobile devices but NGUI is worth a look at. Not sure if its still free but it makes setting up UIs easier imo.

Hope this helps.

TheNoob
  • 861
  • 2
  • 11
  • 25
  • Thank you for advise,button is not working in unity remoto scren,but after i build my phone,it's working. thank you for advise – ugur albayrak Mar 06 '15 at 22:40
0

In "Build Settings" under File menu in Unity, you have to add your level to the lists of levels there.

Jordi Cruzado
  • 875
  • 6
  • 13
  • i already did it,the problem about if statement.GUI button does not working with touch control. How do fix it? – ugur albayrak Mar 06 '15 at 10:56
  • In that case, I would put the if ( Input.touchCount > 0 ) line inside Update() function, and there I would set a variable to be used in OnGUI – Jordi Cruzado Mar 06 '15 at 11:23
  • it's not working, if ( Input.touchCount > 0 ) is working but ,"if (GUI.Button(Rect(Screen.width/1.12, 0, Screen.width/8, Screen.height/8),"restart")) " button is not working – ugur albayrak Mar 06 '15 at 11:57
  • GUI should work. Are you in a level different that 1, right?, I mean, you have to load a different level of that you are now. – Jordi Cruzado Mar 06 '15 at 12:13
  • Yes gui is already working, but button isnot working.Could you give any suggestion for "Restart button" ? – ugur albayrak Mar 06 '15 at 13:09
  • You can not restart the level just loading it again. If you are in level 1, you cannot use LoadLevel(1) to restart it. All depends of the meaning of restart for you. Probably this will involve to reset some variables and to move/delete some GameObjects, perhaps some other operation... I am afraid you have to do it explicitly in a function and call it when the GUI.Button is pressed. – Jordi Cruzado Mar 06 '15 at 14:25