错误CS0120:访问非静态成员`CameraOperator.InventMouseY(float)';需要对象

本文关键字:float 对象 InventMouseY 访问 CS0120 静态成员 CameraOperator 错误 | 更新日期: 2023-09-27 18:28:47

嗨,我正在制作RTS风格的游戏,在选择和突出显示我的车辆时遇到了问题。这是我犯的错误。如有任何帮助,我们将不胜感激。

Assets/Scripts/neneneba Unit2.cs(19,51):错误CS0120:访问非静态成员`CameraOperator.InventMouseY(float)'需要对象引用

这是剧本。

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Unit2 : MonoBehaviour
{
    public bool selected = false;
    public float floorOffset = 1;
    public float speed = 5;
    public float stopDistanceOffset = 0.5f;
    private Vector3 moveToDest = Vector3.zero;
    private void Update ()
    {
        if (renderer.isVisible && Input.GetMouseButtonDown (0)) 
        {
            Vector3 camPos = Camera.main.WorldToScreenPoint (transform.position);
            camPos.y = CameraOperator.InvertMouseY(camPos.y);  "This Line Error"
            selected = CameraOperator.Selection.Contains (camPos);
            if (selected)
            {
                renderer.material.color = Color.red;
            }
            else 
            {
                renderer.material.color = Color.white;
        }
        if(selected && Input.GetMouseButtonUp(1))
        {
            Vector3 destination = CameraOperator.getDestination();
            if(destination != Vector3.zero)
            {
                moveToDest = destination;
                moveToDest.y += floorOffset;
            }
        }
    }
    UpdateMove();
    }
    private void UpdateMove()
    {
        if((moveToDest != Vector3.zero) && (transform.position != moveToDest))
        {
            Vector3 direction = (moveToDest - transform.position).normalized;
            direction.y = 0;
            transform.rigidbody.velocity = direction * speed;
            if(Vector3.Distance(transform.position, moveToDest) < stopDistanceOffset)
            {
                moveToDest = Vector3.zero;
            }
        }
        else
        {
            transform.rigidbody.velocity = Vector3.zero;
        }
    }
}


Here is the CameraOperator script.
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 ();
        }
        public 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;
                        }
                }
        }
        public 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;
     }
}

错误CS0120:访问非静态成员`CameraOperator.InventMouseY(float)';需要对象

您的InvertMouseY函数没有标记为static,但您试图将其作为静态函数而不是实例成员进行调用。CameraOperator.InvertMouseY(camPos.y)是一个静态调用(通过类型名CameraOperator调用函数,而不是通过CameraOperator的实例)。

由于InvertMouseY函数似乎不依赖于任何类型的状态(仅依赖于其单个输入参数),因此解决此问题的最简单方法是将static添加到函数的声明中。

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