Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 索引越界错误。我怎么修理它?_C#_Unity3d_Touch - Fatal编程技术网

C# 索引越界错误。我怎么修理它?

C# 索引越界错误。我怎么修理它?,c#,unity3d,touch,C#,Unity3d,Touch,这是错误deltaVector=(Vector3)Input.GetTouch(0).position-transform.position 完整代码如下: using UnityEngine; using System.Collections; using UnityEngine.EventSystems; using UnityEngine.UI; public class JoystickController : MonoBehaviour, IPointerUpHandler, IP

这是错误
deltaVector=(Vector3)Input.GetTouch(0).position-transform.position
完整代码如下:

using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class JoystickController : 
MonoBehaviour, IPointerUpHandler, IPointerDownHandler {

        public ControllerInput tankPlayer;
        public float offset = 35.0f;

        public Sprite button_not_touch;
        public Sprite button_touch;

        public Image[] imageButtons;

        private bool isTouch;
        private Vector3 deltaVector;

  void Update()
  {
    // When touch, process touch postion
    if (isTouch) {

      // Check touch, if have mutiple touch
      Touch[] myTouchs = Input.touches;
      Vector3 touchPos = Vector3.zero;

      if (myTouchs.Length > 1) {
        for (int i = 0; i < myTouchs.Length; i++)
        {
          if (myTouchs[i].position.y < transform.position.y * 2 && myTouchs[i].position.x < transform.position.x * 2) {
            touchPos = myTouchs[i].position;
            break;
          }
        }
        deltaVector = touchPos - transform.position;
      }
      else
        deltaVector = (Vector3)Input.GetTouch(0).position - transform.position;
      Debug.Log("log");
      // Process when magnitude delta vector greater than 25
      if (Vector3.Magnitude(deltaVector) > 25.0f) {
        if (Mathf.Abs(deltaVector.x) > Mathf.Abs(deltaVector.y)) {
          // Move horizontal
          if (deltaVector.x > offset) {
            tankPlayer.MoveRight();
            ButtonHit(3);
          } else if (deltaVector.x < -offset) {
            tankPlayer.MoveLeft();
            ButtonHit(1);
          }
        } else {
          // Move vertical
          if (deltaVector.y > offset) {
            tankPlayer.MoveUp();
            ButtonHit(0);
          } else if (deltaVector.y < -offset) {
            tankPlayer.MoveDown();
            ButtonHit(2);
          }
        }
      } else
      tankPlayer.Release();
    } else
      tankPlayer.Release();
  }

  // Method to change button sprites
  void ButtonHit(int indexTouch)
  {
    foreach(Image image in imageButtons)
    image.sprite = button_not_touch;


    imageButtons[indexTouch].sprite = button_touch;
  }

        // Event handle when touch to joystick
        public void OnPointerUp(PointerEventData eventData )
  {

    foreach(Image image in imageButtons)
    image.sprite = button_not_touch;

    isTouch = false;
  }

        // Event handle when touch to joystick
        public void OnPointerDown(PointerEventData eventData)
  {
    isTouch = true;
  }

  void OnDisable()
  {
    tankPlayer.Release();
  }
}
使用UnityEngine;
使用系统集合;
使用UnityEngine.EventSystems;
使用UnityEngine.UI;
公共类JoystickController:
单行为,IPointerUpHandler,IPointerDownHandler{
公共控制员;
公共浮动偏移量=35.0f;
公共精灵按钮不可触摸;
公共精灵按钮(触摸);;
公共图像按钮;
私人布尔伊斯托克;
私有向量3三角向量;
无效更新()
{
//触摸时,处理触摸位置
如果(isTouch){
//检查触摸,如果有多个触摸
Touch[]mytouch=Input.Touch;
Vector3 touchPos=Vector3.0;
如果(mytouch.Length>1){
for(int i=0;i25.0f){
if(Mathf.Abs(deltaVector.x)>Mathf.Abs(deltaVector.y)){
//横移
if(deltaVector.x>偏移量){
tankPlayer.MoveRight();
钮扣(3);
}else if(deltaVector.x<-偏移量){
tankPlayer.MoveLeft();
钮扣(1);
}
}否则{
//垂直移动
if(deltaVector.y>偏移量){
tankPlayer.MoveUp();
ButtonHit(0);
}else if(deltaVector.y<-偏移量){
tankPlayer.MoveDown();
钮扣(2);
}
}
}否则
tankPlayer.Release();
}否则
tankPlayer.Release();
}
//方法更改按钮精灵
void按钮hit(int indexTouch)
{
foreach(图像按钮中的图像)
image.sprite=按钮\未触摸;
imageButtons[indexTouch].sprite=按钮触摸;
}
//触摸操纵杆时的事件句柄
POINTERUP上的公共无效(PointerEventData事件数据)
{
foreach(图像按钮中的图像)
image.sprite=按钮\未触摸;
isTouch=假;
}
//触摸操纵杆时的事件句柄
POINTERDOWN上的公共无效(PointerEventData事件数据)
{
isTouch=true;
}
无效可禁用()
{
tankPlayer.Release();
}
}

您应该检查
mytouch
是否至少有一个元素。实际上,即使
mytouch
为空,您也可以输入else分支,这会引发异常。

您应该检查
mytouch
是否至少有一个元素。实际上,即使myTouchs
为空,也可以输入else分支,这会引发异常。

如果myTouchs.Length<1,则表示myTouchs包含0个元素

因此,如果您输入.GetTouch(0),它将返回null,因为GetTouch数组中没有可用的触摸

在您的else中添加此条件

else if( myTouchs.Length == 1) 
//your code
或者将您的首字母改为

 if (myTouchs.Length >= 1)
更改else应该是这样的

 if (myTouchs.Length > 1) {
    for (int i = 0; i < myTouchs.Length; i++)
    {
      if (myTouchs[i].position.y < transform.position.y * 2 && myTouchs[i].position.x < transform.position.x * 2) {
        touchPos = myTouchs[i].position;
        break;
      }
    }
    deltaVector = touchPos - transform.position;
  }
  else if( myTouchs.Length == 1) 
if(mytouch.Length>1){
for(int i=0;i
如果myTouchs.Length<1,则表示myTouchs包含0个元素

因此,如果您输入.GetTouch(0),它将返回null,因为GetTouch数组中没有可用的触摸

在您的else中添加此条件

else if( myTouchs.Length == 1) 
//your code
或者将您的首字母改为

 if (myTouchs.Length >= 1)
更改else应该是这样的

 if (myTouchs.Length > 1) {
    for (int i = 0; i < myTouchs.Length; i++)
    {
      if (myTouchs[i].position.y < transform.position.y * 2 && myTouchs[i].position.x < transform.position.x * 2) {
        touchPos = myTouchs[i].position;
        break;
      }
    }
    deltaVector = touchPos - transform.position;
  }
  else if( myTouchs.Length == 1) 
if(mytouch.Length>1){
for(int i=0;i
非常感谢Andrea,你可以修复代码帮助我,我已经修复了,但没有;)非常感谢Andrea,你可以修复代码帮助我,我已经修复了,但是没有(((