Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by alb917 · Apr 22, 2013 at 03:00 PM · buttonscenetouchscreenloadlevel

Touch button to load scene, instead of touching anywhere on screen

I'm creating a 2D game for iOS using Sprites, and have a 'Play' button on the main menu scene. I want to load the next screen when the play button is tapped, and have used the following script:

 function Update(){
 if ((Input.touchCount == 1) &&
          (Input.GetTouch(0).phase == TouchPhase.Began) )
     {
     Application.LoadLevel("level1");
     }
     }



The code works fine, but when the screen is tapped anywhere, 'level1' is loaded. I just the button to register the input. I attached the code to the sprite which I am using for my button, and also tried attaching it to a gameobject infront of the sprite, but with no mesh renderer. I can still tap anywhere on the screen to load the next level, but only want to load level if the button is pressed.

Any ideas how to prevent this?

Thanks.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image tkos · Jun 23, 2014 at 04:19 PM 0
Share

@ahaykal hi i was having the same issue and tried your code but I still find that level loads no matter where on screen i touch. Also, I have three buttons, each selects a different player. I wrote three different scripts, one for each button and placed it on relevant game object, but no matter where on the screen i touch, always the same level will load. Please help

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by ahaykal · Apr 22, 2013 at 03:55 PM

 function Update () {
       for(var i:int = 0; i < Input.touches.Length; i++)//How many touches do we have?
     {
         var touch:Touch = Input.touches[i];//The touch
         var ray:Ray = Camera.main.ScreenPointToRay(touch.position);
         var hit:RaycastHit = new RaycastHit();
 
         if(Physics.Raycast(ray,hit, 1000))
         {
             if(hit.collider.gameObject == this.gameObject)
             {
                 switch(touch.phase)
                 {
                     case TouchPhase.Began://if the touch begins
                       Application.LoadLevel("level1");
                     break;
                  }
             }
          }
 
         }
 
 }

Try this one. It is untested though.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image alb917 · Apr 22, 2013 at 04:19 PM 0
Share

Thanks!! Worked a treat :)

avatar image ahaykal · Apr 23, 2013 at 11:21 AM 0
Share

Could you mark it as solved please

avatar image Wolfspeed2166 · Jan 16, 2018 at 11:54 PM 0
Share

can somebody translate this in to C+ please

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Change to scene based on current scene 1 Answer

Touch controls not working after reloading a scene. 1 Answer

Android: Scene change (Application.LoadLevel) has no effect 3 Answers

LoadLevel easy i know, but not working. 1 Answer

How to make a character move towards a side of the screen that's pressed at a constant rate? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges