- Home /
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.
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.
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");
}
}
i think down voter didnt understood what is input.touchcount.
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?
@whyddoidoit bcoz i wrote code directly here not in an editor. Thats why missed capital.
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.
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.
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).
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);
}
}
}
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.