H,我要开发一个带有 unity 的拖放游戏但是出现这个错误,请问如何解决这个错误!
这是完整的错误 (错误 CS1061:“GameObject”不包含“localPosition”的定义,并且找不到接受“GameObject”类型的第一个参数的可访问扩展方法“localPosition”(您是否缺少 using 指令或程序集引用?))
这是我在 C# 脚本中的代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveSystem : MonoBehaviour
{
public GameObject correctForm;
private bool moving; // to check if it is moving or not
private float startPosX;
private float startPosY;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if(moving){
Vector3 mousePos;
mousePos= Input.mousePosition;
mousePos = Camera.main.ScreenToWorldPoint(mousePos);
this.gameObject.transform.localPosition = new Vector3(mousePos.x - startPosX, mousePos.y - startPosY, this.gameObject.localPosition.z);
}
}
public void OnMouseUp(){
moving = false;
}
public void OnMouseDown(){
if (Input.GetMouseButtonDown(0))
{
Vector3 mousePos;
mousePos = Input.mousePosition;
mousePos = Camera.main.ScreenToWorldPoint(mousePos);
startPosX = mousePos.x - this.transform.localPosition.x;
startPosY = mousePos.y - this.transform.localPosition.y;
moving = true;
}
}
}
答案 0 :(得分:1)
在更新函数中,在最后一行的最后一个参数中,您输入了this.gameObject.localPosition.z。 GameObject 没有名为 localPosition 的字段。您应该通过this.gameObject.transform.localPosition.z 修复它。总之,您的更新应如下所示:
// Update is called once per frame
void Update()
{
if(moving){
Vector3 mousePos;
mousePos= Input.mousePosition;
mousePos = Camera.main.ScreenToWorldPoint(mousePos);
this.gameObject.transform.localPosition = new Vector3(mousePos.x - startPosX, mousePos.y - startPosY, this.gameObject.transform.localPosition.z);
}
}