unity c# - IndexOutOfRangeException
So I made a little game that has a problem. As soon as I load the game, I get errors. Lots of them.
I can't seem to find the problem in this code:
//This is where the exception is
Touch touch = Input.touches [0];
float TouchTime;
if (touch.phase == TouchPhase.Began) {
TouchTime = Time.time;
}
if (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled) {
if (Time.time - TouchTime <= 0.5) {
//response to tap
print ("");
}
else {
print ("Long press was" + TouchTime + "seconds long.");
}
}
Also this was in the update()
method, which might explain the huge number of errors, but how do I fix this?
Answer by tanoshimi · Sep 03, 2016 at 06:17 AM
Are you always touching the screen? If not, there won't be any touches in the Input.touches array, so attempting to access Input.touches[0]
will produce the exception described.
Your answer
Follow this Question
Related Questions
the type initializer for "Hypixel.NET.Hypixel.Api" threw an exception 0 Answers
NullReferenceException being set off when it shouldn't be? 1 Answer
File.Move won't work : IOException: Win32 IO returned ERROR_ALREADY_EXISTS. Path: 0 Answers
Unhandled Exception: System.StackOverflowException when using Switch Statement? 1 Answer
Enoent error: diskusage.node not found when starting Unity Hub 0 Answers