- Home /
how do i use touch.Position
Im new in android scripting but I need to get the position of the touch on the screen but if i use Im using javascript
Touch.position
im getting an error : "BCE0020: An instance of type 'UnityEngine.Touch' is required to access non static member 'position'."
Script:
pragma strict
var ton : AudioClip; var textur : Texture; var hitpos : Vector2; var ja : int = 1;
function Start () {
}
function Update () {
if(Input.GetTouch(0).phase==TouchPhase.Began){
hitmarker(); hitpos =
Touch.position; }
}
function hitmarker(){ audio.PlayOneShot(ton); }
how can i fix this thanks in andvantage
Answer by Habitablaba · Oct 28, 2014 at 08:53 PM
The error is complaining that you are attempting to access a property on an object as if it were a static property on a class.
What you need to do is have an instance of the Touch class through which you can access the position property.
You are already getting an instance of the class, actually, but you're tossing it out immediately.
Consider the following:
function Update () {
var touch = Input.GetTouch(0);
if(touch != null && touch.phase==TouchPhase.Began){
hitmarker();
hitpos = touch.position;
}
}
Here, you get the touch object form Input.GetTouch(0) and save it in a variable.
Then you check that variable to make sure someone touched, then to make sure the phase is right.
After that you can get the position form the variable (notice the little 't' instead of the big 'T').