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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Button if not clicked error

Discussion in 'Scripting' started by nicholashallwork, Jul 17, 2017.

  1. nicholashallwork

    nicholashallwork

    Joined:
    May 24, 2017
    Posts:
    16
    Hello, I am new to unity and am trying to set up so that if you click a button you go to a new level. For the base functionality i want to get before i get animations and other stuff in. I have ran into a problem and i was hoping someone could explain why this is happening. I am getting an error for Unexpected Symbol '{' on line 25 of my main scenario script. I will post both my scripts.

    Main Scenerio Scprit
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using System.Linq;
    5.  
    6. public class TwoPaths : MonoBehaviour
    7. {
    8.  
    9.     private GameObject ShinyWood;
    10.     private GameObject Dude;
    11.  
    12.  
    13.  
    14.     void Start()
    15.     {
    16.      
    17.     }
    18.  
    19.  
    20.     void Update()
    21.     {
    22.  
    23.  
    24.         if (Input.GetTouch("!GoToSceneButton")
    25.         {
    26.             Application.LoadLevel("GameOver");
    27.         }
    28.     }
    29. }
    Button Script
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.SceneManagement;
    4.  
    5. public class GoToSceneButton : MonoBehaviour
    6. {
    7.  
    8.     public void GoToScene(string ButtonGames)
    9.     {
    10.         SceneManager.LoadScene("Start");
    11.     }
    12. }
     
  2. cstooch

    cstooch

    Joined:
    Apr 16, 2014
    Posts:
    354
    The problems are on line 24 of that script:
    Code (csharp):
    1. if (Input.GetTouch("!GoToSceneButton")
    One, you're missing a close parenthesis, and that's what the error is telling you, in an obscure way.

    Two, you have a "!" in the Input.GetTouch.. and well, I haven't worked with touch controls in a long time, but I don't think "GoToSceneButton" even makes sense as a parameter either. It's expecting an index, and I think the index represents the number of fingers touching the screen. Anyways, you'll have to re-think what you're doing there, as I don't think that isn't going to work, but I'm not sure. Like I said, been a while since I've worked with touch controls, so I'll let someone else help with questions you might have on that.
     
  3. Phorsaken

    Phorsaken

    Joined:
    Dec 12, 2013
    Posts:
    27
    Code (CSharp):
    1. if (Input.GetTouch("GoToSceneButton")
    2. {
    3.  Application.LoadLevel("GameOver");
    4. }
    should be
    Code (CSharp):
    1.  
    2. f (Input.GetTouch("GoToSceneButton"))
    3. {
    4.  Application.LoadLevel("GameOver");
    5. }
    6.  
    You were just missing a ) is all. Also you should us SceneManager now to load levels.

    -Alexander
     
  4. nicholashallwork

    nicholashallwork

    Joined:
    May 24, 2017
    Posts:
    16
    GoToSceneButton is a button the second script i listed. I am trying to get it so that if someone doesn't click that button it goes to a game over screen. I thought ! might work though am coming from a different programming background so maybe i'm mixing things. Thanks for the help guys I have concluded I am blind. As expected another errors for having ! as an invalid and the button has to be an int. Does anyone mind explaining to me the best way to have a game over scene load a click/touch goes through not on the button?
     
    Last edited: Jul 17, 2017
  5. cstooch

    cstooch

    Joined:
    Apr 16, 2014
    Posts:
    354
    Well, re the use of the not operator (!), C# does use this as well, but I don't think I know of a language where it makes sense to use it in a string literal like you did. It would be more like if (!SomeFunction(someParameter))

    Sorry, on mobile, so I can't type a lot here,but I'd really, really recommend doing some tutorials first. There's some UI ones out there as well as mobile ones (since you were using touch controls in your code). Otherwise I can see you posting a million forum posts as people walk you through it all. There's a lot on YouTube and on the Unity home page under learn.
     
    Last edited: Jul 17, 2017
  6. nicholashallwork

    nicholashallwork

    Joined:
    May 24, 2017
    Posts:
    16
    Sorry i thought it would work in a not with a button. I have watched a few tutorials on UI and touch controls. None of them dealt with an input that doesn't hit something though. If you have any recommendations for tutorials covering this and other stuff i would love to watch them. Thank you for your help and time I really appreciate it.
     
  7. cstooch

    cstooch

    Joined:
    Apr 16, 2014
    Posts:
    354
    Sorry, I'm pretty rusty with the UI, so I'm probably not the best to answer that. Someone might have a more "elegant" solution, but this should work:

    You could have a flag on the button script that indicates whether the button has been pressed or not. You can set up an On Click () event (in property inspector for the button) that just calls a function that sets the flag to true. In your update function for that button.. check if !flag (replace flag with your flag name)... if so, game over.. otherwise, reset the flag to false for the next turn through.

    Re: tutorials... I didn't realize you'd already done some. You probably won't get much more out of some more of the UI ones, although I think for UI the Unity official ones are decent. It's probably hard to find one that handles scenario where you are not pressing the button, like you are saying. I suggested tutorials because you seemed a little confused how the not (!) operator would work. It's meant specifically to use with booleans / functions returning a bool.