簡體   English   中英

錯誤 CS1061:“GameObject”不包含“localPosition”的定義

[英]error CS1061: 'GameObject' does not contain a definition for 'localPosition'

H,我打算用unity開發一個拖放游戲但是出現這個錯誤,請問如何解決這個錯誤!

這是完整的錯誤(錯誤CS1061:'GameObject'不包含'localPosition'的定義,並且找不到接受'GameObject'類型的第一個參數的可訪問擴展方法'localPosition'(您是否缺少使用指令或裝配參考?))

這是完整的錯誤

這是我在 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; 

        }
    }
}
 

在更新 function 中,在最后一行的最后一個參數中,您輸入了 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);
    }
    
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM