-1

Tag "mShape" is the yellow shapes.

Game Work When : * The ball will not create if you clicked over the yellow shape. * Click the red space will create a ball (PointA), When I moves my finger i see path of dots as direction (PointB), When i release my finger, Ball will move.

Game Not Work When : when i keep pressing with my finger over the yellow shape and drag with my finger the touch. I got the bug.

*I used "Log viewer" assets to detect the bug when i play in my device.

CHECK BUG IMAGE

CHECK BUG2 IMAGE

if (ShowDir)
{
    pointB = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);
    pointB.z = Ball.transform.position.z;

    if (DirDectect)
    {

        if (DottedLine.DottedLine.angle < 92 && DottedLine.DottedLine.angle > 88)
        {
            AudioSource my_audio = GameObject.Find("SDir").GetComponent<AudioSource>();
            my_audio.Play();
            pointA.Set(MyBall.GetComponent<Transform>().position.x, pointA.y, pointA.z);
            pointB.Set(MyBall.GetComponent<Transform>().position.x, pointB.y, pointB.z);
            //DirDectect = false;
            Invoke("DirDet", 0.7f);
        }

        if (DottedLine.DottedLine.angle < 272 && DottedLine.DottedLine.angle > 268)
        {
            AudioSource my_audio = GameObject.Find("SDir").GetComponent<AudioSource>();
            my_audio.Play();
            pointA.Set(MyBall.GetComponent<Transform>().position.x, pointA.y, pointA.z);
            pointB.Set(MyBall.GetComponent<Transform>().position.x, pointB.y, pointB.z);
            //DirDectect = false;
            Invoke("DirDet", 0.7f);
        }

        if (DottedLine.DottedLine.angle < 2 || DottedLine.DottedLine.angle > 358)
        {
            AudioSource my_audio = GameObject.Find("SDir").GetComponent<AudioSource>();
            my_audio.Play();
            pointA.Set(pointA.x, MyBall.GetComponent<Transform>().position.y, pointA.z);
            pointB.Set(pointB.x, MyBall.GetComponent<Transform>().position.y, pointB.z);
            //DirDectect = false; 
            Invoke("DirDet", 0.7f);
        }

        if (DottedLine.DottedLine.angle < 182 && DottedLine.DottedLine.angle > 178)
        {
            AudioSource my_audio = GameObject.Find("SDir").GetComponent<AudioSource>();
            my_audio.Play();
            pointA.Set(pointA.x, MyBall.GetComponent<Transform>().position.y, pointA.z);
            pointB.Set(pointB.x, MyBall.GetComponent<Transform>().position.y, pointB.z);
            //DirDectect = false;
            Invoke("DirDet", 0.7f);
        }
    }

    if (DottedLine.DottedLine.angle > 2 && DottedLine.DottedLine.angle < 88 ||
        DottedLine.DottedLine.angle > 92 && DottedLine.DottedLine.angle < 178 ||
        DottedLine.DottedLine.angle > 182 && DottedLine.DottedLine.angle < 268 ||
        DottedLine.DottedLine.angle > 272 && DottedLine.DottedLine.angle < 358)
    {

        DirDectect = true;
    }

    DottedLine.DottedLine.Instance.DrawDottedLine(pointA, pointB);
}

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

    if (!EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId))
    {
         Vector2 worldPoint = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);
         RaycastHit2D hit = Physics2D.Raycast(worldPoint, Camera.main.transform.forward);

         if (hit.collider.tag != null)
         {
             if (touch.phase == TouchPhase.Began && hit.collider.tag != "mShape")
             {
                 pointA = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);
                 MyBall = Instantiate(Ball, new Vector2(pointA.x, pointA.y), Quaternion.identity);
             }

             if (touch.phase == TouchPhase.Moved && hit.collider.tag != "mShape")
             {
                 ShowDir = true;
             }

             if (touch.phase == TouchPhase.Ended && hit.collider.tag != "mShape")
             {

                 if (wrapActive)
                 {
                     MyBall.AddComponent<wrap>();
                 }

                 MyBall.GetComponent<Rigidbody2D>().velocity = (pointB - MyBall.transform.position).normalized * speed;

                 HintShow.SetActive(false);
                 HintBtn.interactable = false;
                 SkipBtn.interactable = false;

                 ShowDir = false;
                 DesTime = true;
             }
         }  
    }
}
else
{
    Debug.Log("NULL");
}

SOLUTION

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

            if(ShowDir)
        {
            pointB = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);
            pointB.z = Ball.transform.position.z;

            if (DirDectect)
            {

                if (DottedLine.DottedLine.angle < 92 && DottedLine.DottedLine.angle > 88)
                {
                    AudioSource my_audio = GameObject.Find("SDir").GetComponent<AudioSource>();
                    my_audio.Play();
                    pointA.Set(MyBall.GetComponent<Transform>().position.x, pointA.y, pointA.z);
                    pointB.Set(MyBall.GetComponent<Transform>().position.x, pointB.y, pointB.z);

                    Invoke("DirDet", 0.7f);
                }

                if (DottedLine.DottedLine.angle < 272 && DottedLine.DottedLine.angle > 268)
                {
                    AudioSource my_audio = GameObject.Find("SDir").GetComponent<AudioSource>();
                    my_audio.Play();
                    pointA.Set(MyBall.GetComponent<Transform>().position.x, pointA.y, pointA.z);
                    pointB.Set(MyBall.GetComponent<Transform>().position.x, pointB.y, pointB.z);

                    Invoke("DirDet", 0.7f);
                }

                if (DottedLine.DottedLine.angle < 2 || DottedLine.DottedLine.angle > 358)
                {
                    AudioSource my_audio = GameObject.Find("SDir").GetComponent<AudioSource>();
                    my_audio.Play();
                    pointA.Set(pointA.x, MyBall.GetComponent<Transform>().position.y, pointA.z);
                    pointB.Set(pointB.x, MyBall.GetComponent<Transform>().position.y, pointB.z);

                    Invoke("DirDet", 0.7f);
                }

                if (DottedLine.DottedLine.angle < 182 && DottedLine.DottedLine.angle > 178)
                {
                    AudioSource my_audio = GameObject.Find("SDir").GetComponent<AudioSource>();
                    my_audio.Play();
                    pointA.Set(pointA.x, MyBall.GetComponent<Transform>().position.y, pointA.z);
                    pointB.Set(pointB.x, MyBall.GetComponent<Transform>().position.y, pointB.z);

                    Invoke("DirDet", 0.7f);
                }
            }

            if (DottedLine.DottedLine.angle > 2 && DottedLine.DottedLine.angle < 88 ||
                DottedLine.DottedLine.angle > 92 && DottedLine.DottedLine.angle < 178 ||
                DottedLine.DottedLine.angle > 182 && DottedLine.DottedLine.angle < 268 ||
                DottedLine.DottedLine.angle > 272 && DottedLine.DottedLine.angle < 358)
            {

                DirDectect = true;
            }

            DottedLine.DottedLine.Instance.DrawDottedLine(pointA, pointB);

        }

            if(!EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId)){

                Vector2 worldPoint = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);
                RaycastHit2D hit = Physics2D.Raycast(worldPoint, Camera.main.transform.forward);

                 if (hit.collider.tag != null ){

                        //BEGIN
                        if (touch.phase == TouchPhase.Began && hit.collider.tag != "mShape" && MoveFalse )
                        {
                            BlockCode = false;
                            pointA = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);
                            MyBall = Instantiate(Ball, new Vector2(pointA.x, pointA.y), Quaternion.identity);
                        }

                        if (touch.phase == TouchPhase.Began && hit.collider.tag == "mShape" && MoveFalse)
                        {
                           BlockCode = true;
                        }

                        if (!BlockCode)
                    {
                    //MOVE
                    if (touch.phase == TouchPhase.Moved && hit.collider.tag != "mShape")
                    {

                        ShowDir = true;
                        mShapeClicked = false;
                        MoveFalse = false;
                        Debug.Log("MOVE");
                    }

                    if (touch.phase == TouchPhase.Moved && hit.collider.tag == "mShape")
                    {
                        ShowDir = true;
                        mShapeClicked = true;
                        MoveFalse = false;
                        Debug.Log("OverShape");
                    }

                    //NO-MOVE
                    if (touch.phase == TouchPhase.Stationary && hit.collider.tag != "mShape")
                    {
                        ShowDir = true;
                        mShapeClicked = false;
                        MoveFalse = false;
                        Debug.Log("NoMove");
                    }

                    if (touch.phase == TouchPhase.Stationary && hit.collider.tag == "mShape")
                    {
                        ShowDir = true;
                        mShapeClicked = true;
                        MoveFalse = false;
                        Debug.Log("NoMove");
                    }

                    //END 
                    if (touch.phase == TouchPhase.Ended)
                    {
                        MoveFalse = false;
                        mShapeClicked = false;
                        ShowDir = false;
                        DesTime = true;

                        Debug.Log("END");

                        if (wrapActive)
                        {
                            MyBall.AddComponent<wrap>();
                        }

                        if (!mShapeClicked)
                        {
                            MyBall.GetComponent<Rigidbody2D>().velocity = (pointB - MyBall.transform.position).normalized * speed;
                        }

                        HintShow.SetActive(false);
                        HintBtn.interactable = false;
                        SkipBtn.interactable = false;



                    }

                   }
                 }

            }
        }
derHugo
  • 83,094
  • 9
  • 75
  • 115

1 Answers1

1

You're getting an "out of bounds" error when you call the function Input.GetTouch(0). That means you're trying to get information about the first touch occurring when there are no touches occurring. You'll need to modify your code to first make sure that there is at least one active touch, using Input.touchCount

ARutherford
  • 116
  • 4
  • Ok, but i already did " if (Input.touchCount > 0) " { Touch touch = Input.GetTouch(0); ..... } .Can you modify my script. Thank you. – Nexus Games May 18 '19 at 17:15
  • 1
    But not in `pointB = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);`, unless there's some code you didn't show. – Olivier Jacot-Descombes May 18 '19 at 17:22
  • Ok, I was really thinking about that too. Yes it can be. I will change now and reply to you with the results. Thank you :) – Nexus Games May 18 '19 at 17:31
  • @OlivierJacot-Descombes right at the top of the code block, `pointB = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);` <-- not inside `if (Input.touchCount > 0)` – Draco18s no longer trusts SE May 18 '19 at 22:27
  • Hello bro :) thank you :). I fixed my game issues. I updated the post with what i did but i'm sure i can improve more :). I will make more updates each time i improves the code. – Nexus Games May 20 '19 at 21:40
  • @NexusGames No, that's not how this community works! You should probably go through the [Tour](https://stackoverflow.com/tour). Do not add the answer as edit to your question but instead mark the answer that helped to solve your specific problem as accepted. If you have another question open a new one. Question about improving working code are offtopic on StackOverflow and should rather be posted to [Code Review](https://codereview.stackexchange.com/). – derHugo May 22 '19 at 04:36