C#声明错误

时间:2014-10-30 06:22:53

标签: c#

我正在制作一个RTS风格的游戏并且有一些错误请帮助。

Assets / Scripts / CameraOperator.cs(64,38):错误CS0023:!' operator cannot be applied to operand of type UnityEngine.Vector3'

Assets / Scripts / CameraOperator.cs(74,38):错误CS0165:使用未分配的局部变量`hit'

这是脚本。

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class CameraOperator : MonoBehaviour
{
    public Texture2D selectionHighlight = null;
    public static Rect Selection = new Rect (0, 0, 0, 0);
    private Vector3 StartClick = -Vector3.one;
    private static Vector3 moveToDestination = Vector3.zero;
    private static List<string> passables = new List<string> () {"Floor"};

    private void Update ()
    {
            CheckCamera ();
            CleanUp ();
    }

    private void CheckCamera ()
    {
            if (Input.GetMouseButtonDown (0)) {
                    StartClick = Input.mousePosition;
            }
            if (Input.GetMouseButtonUp (0)) {
                    StartClick = -Vector3.one;
            }
            if (Input.GetMouseButton (0)) {
                    Selection = new Rect (StartClick.x, InvertMouseY (StartClick.y), Input.mousePosition.x - StartClick.x, InvertMouseY (Input.mousePosition.y) - InvertMouseY (StartClick.y));
                    if (Selection.width < 0) {
                            Selection.x += Selection.width;
                            Selection.width = -Selection.width;
                    }
                    if (Selection.height < 0) {
                            Selection.y += Selection.height;
                            Selection.height = -Selection.height;
                    }
            }
    }

    private void OnGUI ()
    {
            if (StartClick == -Vector3.one) {
                    GUI.color = new Color (1, 1, 1, 0.5f);
                    GUI.DrawTexture (Selection, selectionHighlight);
            }
    }

    float InvertMouseY (float y)
    {
            return Screen.height - y;
    }

    private void CleanUp ()
    {
            if (!Input.GetMouseButtonUp (1)) {
                    moveToDestination = Vector3.zero;
            }
    }

    public static Vector3 getDestination ()
    {
            RaycastHit hit;
            Ray r = Camera.main.ScreenPointToRay (Input.mousePosition); 
            if (moveToDestination = Vector3.zero) {


                    if (Physics.Raycast (r, out hit)) {
                            while (!passables.Contains(hit.transform.gameObject.name)) {
                                    if (!Physics.Raycast (hit.transform.position, r.direction, out hit))
                                            break;
                            }
                    }
            }
            if (!hit.transform == null) {
                    moveToDestination = hit.point;
            }
            return moveToDestination;
    }

}

1 个答案:

答案 0 :(得分:1)

您在函数}

中有一个额外的public static Vector3 GetDestination
public static Vector3 GetDestination ()
{
        if (moveToDestination == Vector3.zero) 
        {
            RaycastHit hit;
            Ray r = Camera.mainCamera.ScreenPointToRay (Input.mousePosition);

            if (Physics.Raycast (r, out hit)) 
            {
                while (!passables.Contains(hit.transform.gameobject.name))
                {
                    if (!Physics.Raycast (hit.transform.position, r.direction, out hit))
                        break;
                    } // This seems like an unneeded }

                }
            }
        }
        if(!hit.transform = null) 
        {
            moveToDestination = hit.point;
        }
    }
    return moveToDestination;
}

编译器将您的代码视为:

public static Vector3 GetDestination ()
{
   if (moveToDestination == Vector3.zero) 
   {
      RaycastHit hit;
      Ray r = Camera.mainCamera.ScreenPointToRay (Input.mousePosition);

      if (Physics.Raycast (r, out hit)) 
      {
         while (!passables.Contains(hit.transform.gameobject.name))
         {
            if (!Physics.Raycast (hit.transform.position, r.direction, out hit))
               break;
         }
      }
   }
}

if(!hit.transform = null) 
{
   moveToDestination = hit.point;
}
}
return moveToDestination;

这应该解释编译器错误。