Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Conall O · May 08, 2015 at 08:27 PM · touchupdatefixedupdate

Solving Index Out of Bounds on Input.GetTouch(0) in an if statement

Hi everyone, hope you can help me with this.

I've been trying to script a simple game where a player throws 3D Junk into a rubbish bin using swipe gestures on a touch device.

I'm getting an error "Index out of Bounds on line 74 of the following script.

"ArgumentException: Index out of bounds. ShootFINAL.FixedUpdate () (at Assets/Scripts/ShootFINAL.cs:74) " can someone explain what the problem is? Its probably something simple!

 using UnityEngine;
 using System.Collections;
 
 /* Contained Logic:
  * Touch Detection 
  * Shot Detection/restrictions
  * Swipe debugging(disabled)
  * 
  * Author: Conall Ó
 */
 
 
 public class ShootFINAL : MonoBehaviour {
 
 private Vector3 touchStart; 
 private Vector3 touchEnd;
 private GameObject lineRenderer;
     
 public GameObject cokecan;
 private GameObject cokecanClone;
 public Vector3 ballPos;
 private bool throwable = true;
 public GameObject availableShotsGO;
 private int availableShots = 10;
 
 public GameObject gameOver;
 public float restartDelay = 4f;
 float restartTimer;
 private bool restarting = false;
     // Use this for initialization
     void Start()
     {
         /* Increase  vertical Gravity */
         Physics.gravity = new Vector3(0, -25, 0);
          
     }
 
     void OnGUI () // ignore this debugging stuff.
     {
         
         foreach (Touch touch in Input.touches) {
             string message = "";
             message += "Finger ID: " + touch.fingerId + "\n";
             message += "Phase: " + touch.phase.ToString () + "\n";
             message += "TapCount: " + touch.tapCount + "\n";
             message += "Pos X: " + touch.position.x + "\n";
             message += "Pos Y: " + touch.position.y + "\n";
             message += "Input.TouchCount: " +Input.touchCount + "\n";
             int num = touch.fingerId;
             GUI.Label (new Rect (0 + 130 * num, 0, 120, 100), message);
             
         }
 
     }
     void FixedUpdate () {
 
 
     if (Input.touchCount > 0) {
 // if there is a touch,    then execute.    
             Debug.Log ("Touchcount >0 detected");
         
 
             if (throwable == true) {
 
                 throwable = false; //set the ball to "thrown" so we can't throw two at the same time
                 availableShots--; // decrement number of shots left
                 availableShotsGO.GetComponent<GUIText> ().text = availableShots.ToString (); // Add the shots remaning as text to our GUI.
                 Debug.Log ("Shot Detected");
     
             }
         }
 
 
             if (Input.GetTouch(0).phase == TouchPhase.Began) {
                 touchStart = Input.GetTouch(0).position;
             }
             
             if (Input.GetTouch(0).phase == TouchPhase.Ended || Input.GetTouch(0).phase == TouchPhase.Canceled) {
                 touchEnd = Input.GetTouch(0).position;
                 DoInput ();
             }
 
 
         
         if (Input.GetButtonDown ("Fire1")) {
             touchStart = Input.mousePosition;
         }
         
         if (Input.GetButtonUp ("Fire1")) {
             touchEnd = Input.mousePosition;
             DoInput ();
         }
 
 
 }
     void Update()
     {
 
         if (Input.touchCount > 0)
             Debug.Log ("Touches detected: " + Input.touchCount);
 
         /* Check if out of shots / Game over */
         
         if (availableShots <= 0) {
             Application.LoadLevel("GameOver");
 
 
             if (restartTimer >= restartDelay) {
                 Application.LoadLevel (Application.loadedLevel);
                 restarting = false;
             }
             
         }
 
     }
     
     void DoInput()
     {
         Vector3 p1 = new Vector3();
         Vector3 p2 = new Vector3();
         touchStart.z = Camera.main.nearClipPlane+.1f; //push it a little past the clip plane for camera, just to make sure its visible on screen
         touchEnd.z = Camera.main.nearClipPlane+.1f;
         p1 = Camera.main.ScreenToWorldPoint(touchStart);
         p2 = Camera.main.ScreenToWorldPoint(touchEnd);
         
         // CreateLine(p1,p2); - Swipe Debugging line creation on input - SWIPE DEBUGGING
         CreateLine (p1, p2);
 
         Vector3 v = p2-p1;
 
         if (throwable == true) {
             cokecanClone = Instantiate (cokecan, ballPos, transform.rotation) as GameObject;
             cokecanClone.GetComponent<Rigidbody> ().AddForce (v.x * 100, v.y * 120, v.y * 300, ForceMode.Impulse);
             throwable = false;
 
         }
     }
     
 
     //     SWIPE DEBUGGING
     //creates an ugly purple line from pos1 to pos2 in worldspace
     void CreateLine(Vector3 p1, Vector3 p2)
     {
         Destroy(lineRenderer);
         lineRenderer = new GameObject();
         lineRenderer.name = "LineRenderer";
         LineRenderer lr = (LineRenderer)lineRenderer.AddComponent(typeof(LineRenderer));
         lr.SetVertexCount(2);
         lr.SetWidth(0.001f, 0.001f);
         lr.SetPosition(0, p1);
         lr.SetPosition(1, p2);
     }
 
 
 
 
 
     void restart()
     {
         Application.LoadLevel(Application.loadedLevel);
     }
 
 }
 

Comment
Add comment · Show 3
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 Eric5h5 · May 11, 2015 at 03:54 AM 0
Share

You should not be using FixedUpdate. That's only for physics, and it will sometimes miss events such as TouchPhase.Began/GetButtonDown, since those are true for one frame only, and FixedUpdate doesn't run every frame.

avatar image Conall O · May 11, 2015 at 10:26 AM 0
Share

I want my method to be called every fixed framerate frame....

I guess i'l have to split my code up a bit more

avatar image Eric5h5 · May 11, 2015 at 04:43 PM 0
Share

What method? I can't see anything which should be in FixedUpdate. Just use Update.

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Conall O · May 11, 2015 at 11:14 PM

yeah, nevermind, I moved everything to update, ive solved this by rewriting from scratch :)

Comment
Add comment · 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
1

Answer by hbalint1 · May 08, 2015 at 08:28 PM

Change this:

 Touch t = Input.touches[0];

to

 Touch t = Input.GetTouch(0);

EDIT:

this part of your code:

 if (Input.touchCount == 0

means that "if there aren't any touch". I don't think you want that. change it to:

 if (Input.touchCount > 0

Comment
Add comment · Show 4 · 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 Conall O · May 10, 2015 at 01:41 PM 0
Share

it complains the index is now out of bounds, what's going on here?

avatar image hbalint1 · May 10, 2015 at 01:54 PM 1
Share

what line is this error on? anyway this part of your code: if (Input.touchCount == 0 means that "if there aren't any touch". I don't think you want that. change it to > 0

avatar image Bunny83 · May 10, 2015 at 02:03 PM 0
Share

@hbalint1:
You should add this comment to your answer since that's the actual problem ^^. It doesn't really matter if you use Input.touches or Input.GetTouch. Input.touches probably has a bit more overhead as it creates a temp array.

avatar image Conall O · May 11, 2015 at 02:02 AM 0
Share

updated the question, I meant to have >0 in it earlier, oops !

avatar image
0

Answer by ahmedaniss · May 06, 2021 at 02:06 PM

Problem fixed here : https://youtu.be/bC15rUSJ_do

Comment
Add comment · 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

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Interpolation with jumping - Acting Weird. 1 Answer

FixedUpdate limits: consistant 0.01 s on mobile devices? 1 Answer

FixedUpdate performance doubts 2 Answers

Placing Input.GetkeyDown() inside FixedUpdate() slows down the rate I can fire at 1 Answer

how to achieve a LateFixedUpdate effect 5 Answers


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