- Home /
How to check if a variable contains a Touch?
I have variable named aimFinger of type Touch who's existence I want to check in an if statement. I have tried doing "if aimFinger == null" or "if(aimFinger)" but they give me an error. So how can I both assign aimFinger to "null", and check if aimFinger has anything in it?
I'm using C# by the way
Answer by Huacanacha · Nov 10, 2013 at 11:36 PM
The Touch type is a struct which means it's stored by value, not reference. Therefore there is no concept of a null reference to a Touch struct. You may be able to use C# Nullable Types if you just want to store a read-only Touch (there are issues with modifying Nullable Type structs from what I've read).
To use the Nullable Type just add ? to the end of the variable type definition like this:
Touch? aimFinger = null;
You could then process all of the touch inputs and only assign something to aimFinger if the correct touch with the correct touchId exists in Input.touches. I'm guessing you are trying to do something like that.
Note: I haven't played much with nullable types... this is untested and going off the documentation so questioner beware :)
I have not had time to test this yet, but it's a very interesting idea. I'm current;y assigning a Touch to aimFinger, so that may cause problems. I took a glance at the article you referenced, but not thoroughly enough. I'll update you when I get a chance to sit down and do it