Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

On press - open, on press again - close

Discussion in 'Scripting' started by Rutenis, Nov 28, 2014.

  1. Rutenis

    Rutenis

    Joined:
    Aug 7, 2013
    Posts:
    297
    So hey, how do i do this, so whenever i press a button the inventory would show up and when i press again it would close. Do i use booleans? ive tried this:
    Code (csharp):
    1.  
    2. Bool = !Bool;
    But in this case it doesnt work. Is there any other way of doing this?
     
  2. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
    Can you post the actual script? The code snippet you posted there makes no sense. It's not clear if that's the exact thing you've written or the concept you have used.
     
  3. Rutenis

    Rutenis

    Joined:
    Aug 7, 2013
    Posts:
    297
    It doesnt? Well maybe i should've showed you the script earlier. Lol

    Code (csharp):
    1.  
    2. foreach (Touch touch in Input.touches)
    3.         {
    4.             Vector3 inputGuiPosition = touch.position;
    5.             inputGuiPosition.y = Screen.height - inputGuiPosition.y;
    6.          
    7.             if(Input.touchCount > 0)
    8.             {
    9.                 if(backPabckRect.Contains(inputGuiPosition))
    10.                 {
    11.                     ShowInventory = !ShowInventory;
    12.                 }
    13.             }
    14.         }
     
    Last edited: Nov 28, 2014
  4. Defero

    Defero

    Joined:
    Jul 9, 2012
    Posts:
    200
    There is no need to iterate through all the touches if the only thing you need is touchCount. Just do:

    Code (CSharp):
    1. if(Input.touchCount > 0)
    2.             {
    3.                 if(backPabckRect.Contains(inputGuiPosition))
    4.                 {
    5.                     ShowInventory = !ShowInventory;
    6.                 }
    7.             }
    In your case you would have to look at phases of the touch here
     
    djfunkey likes this.