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 gilian · May 31, 2012 at 04:12 PM · androidnexus

function Touch

Hello, all. I have a question. I got this script:

function Touch() { Application.LoadLevel("marble game 2"); }

It's for my android device but nothing happends. Is this a bad script or do i have another thing In place of the Touch? Ty, already.

Comment
Add comment
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

5 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by tomka · Jun 01, 2012 at 04:15 AM

That's not how you do touches.

This previous post on UnityAnswers answers your question nicely: http://answers.unity3d.com/questions/10286/simple-touch-command-on-iphone.html

It is specifically for iPhone, but the information there should work on Android just as well.

Comment
Add comment · 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
1

Answer by hirenkacha · Jun 01, 2012 at 07:20 AM

You will have to detect touch inside Update ().

 function Update()
 {
 if(input.touchcount>0)
 {
 Application.LoadLevel("marble game 2");
 }
 }
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 hirenkacha · Jun 01, 2012 at 09:48 AM 0
Share

i think down voter didnt understood what is input.touchcount.

avatar image whydoidoit · Jun 01, 2012 at 10:35 AM 1
Share

I presumed that the OP wanted to touch something rather than anything. And hence this code would be confusing, you're right, was a little harsh. Have removed the down vote. Also that is Input with a capital I unless I've totally missed something?

avatar image hirenkacha · Jun 01, 2012 at 03:09 PM 0
Share

@whyddoidoit bcoz i wrote code directly here not in an editor. Thats why missed capital.

avatar image
0

Answer by gilian · Jun 01, 2012 at 03:27 PM

Aggenttom, on that page i really can't fine a solution. Also i need an event like i can choose out of 10 buttons. So i think that turtorial isn't that great if you want to do what i want. Ty, already for helping.

Comment
Add comment · Show 2 · 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 whydoidoit · Jun 01, 2012 at 03:28 PM 0
Share

Can you make these things comments and not an answer - answer means a solution to a problem and not a "reply" here. Use the add new comment button on the right hand side of the screen. Someone will probably delete your non-answer answers.

avatar image whydoidoit · Jun 01, 2012 at 03:32 PM 0
Share

In @agenttom's link there was this code:

 //  OnTouchDown.cs
 //  Allows "On$$anonymous$$ouseDown()" events to work on the iPhone.
 //  Attach to the main camera.
 
 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class OnTouchDown : $$anonymous$$onoBehaviour
 {
     void Update () {
         // Code for On$$anonymous$$ouseDown in the iPhone. Unquote to test.
         RaycastHit hit = new RaycastHit();
         for (int i = 0; i < Input.touchCount; ++i) {
             if (Input.GetTouch(i).phase.Equals(TouchPhase.Began)) {
             // Construct a ray from the current touch coordinates
             Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(i).position);
             if (Physics.Raycast(ray, out hit)) {
                 hit.transform.gameObject.Send$$anonymous$$essage("On$$anonymous$$ouseDown");
               }
            }
        }
     }
 }

Which as it says enables a On$$anonymous$$ouseDown() function to be called on the hit object, even on devices with touch screens...

So add that to a script called OnTouchDown - attach it to an empty game object in your scene or the main camera as it suggests and all of your touchable objects will get an On$$anonymous$$ouseDown call when they are touched. (Presu$$anonymous$$g that the have a collider).

avatar image
0

Answer by alattin321 · Jul 17, 2014 at 11:22 AM

 var menu : GUITexture;
 var i = 0;
 function Update ()
 {
 for (var touch : Touch in Input.touches)
 {
 if(touch.phase == TouchPhase.Stationary && menu.HitTest (touch.position)){
  Application.LoadLevel(i);
  }
 }

}

Comment
Add comment · 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
0

Answer by djpiper28 · Jun 30, 2016 at 05:22 PM

This script adds function OnTouch it acts like function OnMouseDown

// Call this OnTouchDown.cs // Allows "OnMouseDown()" events to work on the iPhone. // Attach to the main camera.

using UnityEngine; using System.Collections; using System.Collections.Generic;

public class OnTouchDown : MonoBehaviour { void Update () { // Code for OnMouseDown in the iPhone. Unquote to test. RaycastHit hit = new RaycastHit(); for (int i = 0; i < Input.touchCount; ++i) { if (Input.GetTouch(i).phase.Equals(TouchPhase.Began)) { // Construct a ray from the current touch coordinates Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(i).position); if (Physics.Raycast(ray, out hit)) { hit.transform.gameObject.SendMessage("OnTouch"); } } } } }

Sorry for bad formatting I tried.

Comment
Add comment · 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

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

9 People are following this question.

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

Related Questions

Gyroscope problem (nexus S, Android: 4.0) 1 Answer

Why my sound it's not attaching to my Flashlight 2 Answers

Android Building problem 1 Answer

Problem with First Person Shooter Script 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 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