Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by AlptugYaman · Oct 14, 2016 at 09:44 AM · boundsargumentexceptionindexof

argumentexception: index out of bounds

Hey,

This is probably a really easy question, but i am new in unity and i cant fixed it. I want to only one touch. if people touch it twice. it becomes 2 touches.

UnityException: Index out of bounds.

can somebody help me?

          void Update()
             {
                 Touch touch =Input.GetTouch(0);
         
                 if (dead)
                 {
                     DCD -= Time.deltaTime;
                     if (DCD <= 0)
                     {
                         if (Input.GetTouch(0).tapCount > 0)
                         {
                             Application.LoadLevel(Application.loadedLevel);
                             VFalse.SetActive(true);
                         }
                     }
                 }
                 else
                 {
                     if (Input.touchCount > 0 && touch.phase == TouchPhase.Began)
                         didFlap = true;
                 }
             }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
4

Answer by doublemax · Oct 14, 2016 at 09:46 AM

With "Input.GetTouch(0)" you're assuming that there is already at list one touch in the list. But this is not guaranteed. Before accessing it, you need to check if "Input.touchCount" is > 0.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image sh_code · Oct 14, 2016 at 10:47 AM 1
Share

yes, exactly, Input.touchCount basically gives you Input.touches array count, so that you know IF you can call Input.GetTouch(), and what's the upper bound you can call it with.

https://docs.unity3d.com/ScriptReference/Input-touchCount.html

Within one frame, imagine person tapping with two fingers: Input.touchCount will be 2 Input.GetTouch(0) will return the (chronologically) info about the first finger/point where user touches the screen, and Input.GetTouch(1) will return the second one.

if, in the next frame, user keeps the two fingers on the screen, and adds another one, Input.touchCount will be 3, and Input.GetTouch(2) will contain info about this new point, while GetTouch(0) and (1) will hold the info about the (progress of) previous two touches.

avatar image AlptugYaman · Oct 14, 2016 at 11:18 AM 0
Share

my english is not perfect so where i must to change ?

avatar image sh_code AlptugYaman · Oct 14, 2016 at 11:54 AM 1
Share

in short, everywhere. rewrite the whole code.

  1. check if there was any touch done

    Touch touch; if(Input.touchCount > 0) touch =Input.GetTouch(0);

  2. then everywhere where you try to access the info about the touch, you need this as the first condition:

    if(touch != null && [anything you need to check for within the touch info])

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I keep an Ortho camera in a specific range when the ortho changes? 3 Answers

Checking if ground is 'level' 1 Answer

Imported mesh bounds size is alwasy too big and pivot point is not aligned 1 Answer

Shader LOD 1 Answer

Bounds NaN issue 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges