how to make multi-touch

I am trying to make multi-touch so if tap on the 3d model it will play a sound I have errors on this script please help thanks in advanced

20586-help.png

using UnityEngine;
using System.Collections;

public class NewBehaviourScript : MonoBehaviour {

void upduate ()
    //is there a touch on screen
	if (Input.touches.Length < 0)
	{
		//if no touches then execute this code
	}
	else()  //if there is a touch
	{
		//loop through all of the touches on the screen
		for(int i =0; i < Input.touchCount; i++ )
		{
			//execute this code for current touch (i)on screen
			if(this.guiTexture.HitTest(Input.GetTouch (i).position ))
			{
				//if current touch hits our guitexture, run this code
				if(Input.GetTouch (i).phase == TouchPhase.Began )
				{
					Debug.Log("the touch has begun on" +this.name);
				}

				if(Input.GetTouch (i).phase == TouchPhase.Ended )
				{
					Debug.Log("the touch has ended on" +this.name);
				}
		}
	}
}
}

This seems very weird: the error messages refer to a script called “touch logic.cs”, but the script above should be saved as NewBehaviourScript.cs - the class name (the name you define right after the keywords public class ) must match the name you use to save the script file; by the way, you should start the script name (and class name) with a capital letter (not required, but it’s a good practice), and never let blank spaces in between - use the “CamelCase” approach, where all words start with a capital letter: you could save the script as TouchLogic.cs and name the class as TouchLogic. But there are other errors too: the function upduate won’t ever execute in this life - it should be Update - and you must enclose the function code in curly braces, Input.touches.Length is zero when there’s no touches, else must not have parenthesis after it etc. The whole thing would look like this:

using UnityEngine;
using System.Collections;
 
public class TouchLogic: MonoBehaviour {
 
  void Update (){ // enclose the Update function in curly braces
    //is there a touch on screen
    if (Input.touches.Length <= 0) // <- no touches means <= 0, not < 0
    {
      //if no touches then execute this code
    }
    else // <- there are no parenthesis after else!
    {
      //loop through all of the touches on the screen
      for (int i=0; i < Input.touchCount; i++ )
      {
        //execute this code for current touch (i)on screen
        if(this.guiTexture.HitTest(Input.GetTouch (i).position ))
        {
          //if current touch hits our guitexture, run this code
          if(Input.GetTouch (i).phase == TouchPhase.Began )
          {
            Debug.Log("the touch has begun on" +this.name);
          }
          if(Input.GetTouch (i).phase == TouchPhase.Ended )
          {
            Debug.Log("the touch has ended on" +this.name);
          }
        }
      }
    }
  }
}

Finally, save this script as TouchLogic.cs and attach it to a GUITexture object, or you will get frustrating Null Reference errors.

EDITED: If you want to play a sound when an object is touched, attach the script below to the camera (add also an AudioSource to the camera):

using UnityEngine;
using System.Collections;

[RequireComponent (typeof (AudioSource))]
[RequireComponent (typeof (Camera))]
 
public class TouchLogic: MonoBehaviour {

  public AudioClip sound; // drag the desired sound here
 
  void Update(){ // enclose the Update function in curly braces
    //is there a touch on screen?
    if (Input.touchCount > 0){
      // check whether this object is being touched:
      for (int i=0; i<Input.touchCount; i++){
        Touch touch = Input.GetTouch(i);
        if (touch.phase == TouchPhase.Began){
          // create a logic ray passing through the touch position
          Ray ray = camera.ScreenPointToRay(touch.position);
          RaycastHit hit;
          // if any collider is under the finger...
          if (Physics.Raycast(ray, out hit)){
            audio.PlayOneShot(sound); // play the sound
          }
        }
      }
    }
  }
}