I’m filling in an array with touch positions but it looks like the co-ordinates aren’t updating when I debug.log or print them. This has worked before but won’t now for whatever reason.
Here’s the relevant bits of script, save me Superman!
var resetTouchCount = false;
var touching : iPhoneTouch;
var rawTouchArray = new Array ();
var aCell : Vector2;
function TouchCountMngr()
{
if (resetTouchCount==true)
{
totalRawTouches = 0;
resetTouchCount = false;
}
else
{
aCell = touching.position;
rawTouchArray.Push (aCell);
totalRawTouches ++;
Debug.Log (rawTouchArray +" = X,Y Position. " + totalRawTouches + "=touches. Reset state should be false, is it? = " + resetTouchCount);
}
}
No, no errors. I was getting one for a while but after I switched all of my variables to just be variables and not be strict or public or anything else the null reference error went away. I thought it was maybe because I’d set a needed variable to something inaccessible but I don’t really understand it as the overall script is assigned to an object through the GUI…?
because I don’t see where you actually assign the touch as such it would normally throw a null ref exception
I thought that it’s assigned to the aCell var? For reference here is the totality of the script, please excuse it’s “personality”. :?
#pragma strict
var multiTouchEnabled = false; //doesn't work, for some reason it still displays multiple touch data when touch.position is printed.
function Start()
{
if ( Application.isEditor )
// This is a small hack to improve the precision of input events coming
// from Unity Remote to the editor.
Time.fixedDeltaTime = 0.01; //I don't see any real benefit from using this but I haven't really tested it
}
var fingerCount = iPhoneInput.touchCount;//shows total number of active fingers, not what I want
//but i'm keeping it until I can figure out if the multitouchEnabled is working.
var totalRawTouches : int; //use this to keep count of touches from each unique touch start
function Update ()
{
for (var touch : iPhoneTouch in iPhoneInput.touches) //iPhoneInput.GetTouch(0) <= this seems to be the way to restrict the touch to the first touch only
{
if (touch.phase != iPhoneTouchPhase.Ended touch.phase != iPhoneTouchPhase.Canceled)
{
TouchCountMngr();
}
else // a new touch is starting, clear everything out
{
resetTouchCount = true;
rawTouchArray.Clear();
Debug.Log ("released, is reset true? " + resetTouchCount + " How many touches? =" + totalRawTouches);
}
}
}
var resetTouchCount = false;
var touching : iPhoneTouch;
var rawTouchArray = new Array (); //be careful when setting var types.
//when this was a private var the script wouldn't run
var aCell : Vector2;//Vector2.zero
var aBlankCell : Vector2;
function TouchCountMngr() // always test true first? or your else wont run?
{
if (resetTouchCount==true)
{
totalRawTouches = 0;
Debug.Log ("Reset, state should be true, is it? = " + resetTouchCount);
resetTouchCount = false;
}
else //money! I am adding the new current touch position to
//an array every time else runs, effectively creating a database of touch positions
//only thing that doesn't work is displaying the position for some reason?
{
aCell = touching.position;
rawTouchArray.Push (aCell);
totalRawTouches ++;
Debug.Log (rawTouchArray.length +" = X,Y,Z Position. " + totalRawTouches + "=touches. Reset state should be false, is it? = " + resetTouchCount);
}
}
With a bunch more tinkering and research I think I figured it out.
Thanks dream for the hint about what to look for but it would have been nice to get more support from the community in general, especially as Unity puts the burden of learning how their scripting system works on the use of this forum.
Anyway, what I did (and seems to work) is get the info back from the touch during an update in a FOR loop:
for (var touch : iPhoneTouch in iPhoneInput.touches)
The var touch can then be accessed but seemingly only while in the FOR loop.
Here’s my full script. On a new touch it captures the touch positions into an array and debug prints current position, size of touches array, and number of simultaneous touches:
#pragma strict
var aCell : Vector2;
var rawTouchArray = new Array ();
var clearArray : boolean;
function Update ()
{
for (var touch : iPhoneTouch in iPhoneInput.touches)
{
// if (touch.phase == iPhoneTouchPhase.Began || touch.phase == iPhoneTouchPhase.Moved)
if (iPhoneInput.GetTouch(0).phase == iPhoneTouchPhase.Began || touch.phase == iPhoneTouchPhase.Moved)
{
ClearArray();
var multiTouchEnabled = false; // this still doesn't work! FARTNUGGETS!!!
var aCell = touch.position;
var fingers = iPhoneInput.touchCount;
rawTouchArray.Push (aCell);
Debug.Log ("Position " + aCell + " Array size " + rawTouchArray.length + "Fingers "+ fingers);
}
else if (iPhoneInput.GetTouch(0).phase == iPhoneTouchPhase.Ended || touch.phase == iPhoneTouchPhase.Canceled)
{
Debug.Log ("released");
clearArray = true;
}
}
}
function ClearArray()
{
if (clearArray)
{
rawTouchArray.Clear();
clearArray = false;
}
}