簡體   English   中英

如何在Unity C#腳本中解決錯誤“ CS0029”?

[英]How can I fix the error “CS0029” in my Unity C# script?

我試圖在我的世界副本中制作一個腳本,但它不斷返回此錯誤:

Assets/Materials/minecraft_assets/WorldGeneration.cs(79,41): error CS0029: Cannot implicitly convert type UnityEngine.GameObject to UnityEngine.Vector3

這是我腳本中所有的代碼(而且我知道代碼很爛):

using UnityEngine;
using System.Collections;

public class Block
{
    public int type;
    public bool vis;

    public Block(int t, bool v)
    {
        type = t;
        vis = v;
    }
}

public class WorldGeneration : MonoBehaviour {

    public static int width = 128;
    public static int depth = 128;
    public static int height = 128;

    public int heightScale = 20;
    public float detailScale = 25.0f;

    public GameObject grassBlock;
    public GameObject sandBlock;
    public GameObject snowBlock;

    Block[,,] worldBlocks = new Block[width, height, depth];

    // Use this for initialization
    void Start ()
{
    int seed = (int) Network.time * 10;
    for (int z = 0; z <= depth; z++) {

        for (int x = 0; x <= width; x++) {

            int y = (int) (Mathf.PerlinNoise((x+seed)/detailScale, (z+seed)/detailScale) * heightScale);
            Vector3 blockPos = new Vector3 (x, y, z);

            CreateBlock (y, blockPos, true);
            while (y > 0) {
                y--;
                blockPos = new Vector3 (x, y, z);
                CreateBlock(y, blockPos, false);    
            }
        }
    }
}

    // Use this for creating blocks
void CreateBlock(int y, Vector3 blockPos, bool create)
{
    if (y > 15) {
        if (create)
            Instantiate (snowBlock, blockPos, Quaternion.identity);
        worldBlocks [(int)blockPos.x, (int)blockPos.y,(int) (blockPos.z)] = new Block (1, create);
    } 
    else if (y > 5) {
        if (create)
            Instantiate (grassBlock, blockPos, Quaternion.identity);
        worldBlocks [(int)blockPos.x, (int)blockPos.y, (int)blockPos.z] = new Block (2, create);
    } 
    else {
        if (create)
            Instantiate (sandBlock, blockPos, Quaternion.identity);
        worldBlocks [(int)blockPos.x, (int)blockPos.y, (int)blockPos.z] = new Block (3, create);
    }
}

    // Update is called once per frame
void Update ()
{
    if (Input.GetMouseButtonDown (0)) {
        RaycastHit hit;
        Ray ray = Camera.main.ScreenPointToRay (new Vector3 (Screen.width / 2.0f, Screen.height / 2.0f, 0));
        if (Physics.Raycast (ray, out hit, 1000.0f)) {

            Vector3 blockPos = hit.transform.gameObject.;

            //This is the bottom block. Don't delete it
            if((int)blockPos.y == 0) return;

            worldBlocks [(int)blockPos.x, (int)blockPos.y, (int)blockPos.z] = null;

            Destroy (hit.transform.gameObject);


            for (int x = -1; x <= 1; x++)
                for (int y = -1; y <= 1; y++)
                    for (int z = -1; z <= 1; z++) {
                        if (!(x == 0 && y == 0 && z == 0)) {
                            Vector3 neighbor = new Vector3 (blockPos.x + x, blockPos.y + y, blockPos.z + z);
                            DrawBlock (neighbor);
                        }
                    }
        }
    }

        // Use this for returing values
void DrawBlock(Vector3 blockPos)
{
    if (worldBlocks [(int)blockPos.x, (int)blockPos.y, (int)blockPos.z] == null)
        return;

    if (!worldBlocks [(int)blockPos.x, (int)blockPos.y, (int)blockPos.z].vis) {

        worldBlocks [(int)blockPos.x, (int)blockPos.y, (int)blockPos.z].vis = true;
        if (worldBlocks [(int)blockPos.x, (int)blockPos.y, (int)blockPos.z].type == 1)
            Instantiate (snowBlock, blockPos, Quaternion.identity);
    } else if (worldBlocks [(int)blockPos.x, (int)blockPos.y, (int)blockPos.z].type == 2)
        Instantiate (grassBlock, blockPos, Quaternion.identity);
    else if (worldBlocks [(int)blockPos.x, (int)blockPos.y, (int)blockPos.z].type == 3)
        Instantiate (sandBlock, blockPos, Quaternion.identity);
    else
        worldBlocks [(int)blockPos.x, (int)blockPos.y, (int)blockPos.z].vis = false;
}

/* End Of Script*/

我嘗試通過將blockPos更改為gameObject來調試它,如下所示:

Vector3 blockPos = (gameObject) hit.transform.gameObject;

但是它返回了1000條錯誤消息!

有什么辦法可以幫助任何人嗎?

我在我的HP Pavilion TouchSmart 14 Sleekbook和MonoDevelop 2.0上使用Unity 5.4.2f2。

根據Unity3D文檔, GameObject繼承自System.Object 另外,根據文檔,在GameObjectVector3上沒有隱式的GameObject運算符重載,因此可以簡單地解決此問題:

GameObject不是Vector3 ,並且不能明確地強制轉換為一個。

有關您的問題的解決方案,請參閱Fredrik的答案。 他似乎對Unity3D有所了解,而不僅僅是像我這樣的.NET;)。

就像異常所說的那樣,您的代碼中有一個錯誤。 您正在嘗試將GameObject保存在Vector3中。

第79行:

 Vector3 blockPos = hit.transform.gameObject.;

這不是工作代碼。 你要:

Vector3 blockPos = hit.transform.position;

暫無
暫無

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

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