如何修复我的 Unity C# 脚本中的错误 "CS0029"?
How can I fix the error "CS0029" in my Unity C# script?
我试图在我的 minecraft 克隆中制作脚本,但它一直返回此错误:
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
。此外,根据文档,GameObject
或 Vector3
上没有隐式转换运算符重载,将此问题留给一个简单的答案:
A GameObject
不是 Vector3
,不能明确地转换为一个。
有关您的问题的解决方案,请参阅 Fredrik 的回答。他似乎对 Unity3D 有所了解,而不仅仅是像我一样了解 .NET ;)。
如异常所述,您的代码有错误。您正在尝试将 GameObject 保存在 Vector3 中。
第 79 行:
Vector3 blockPos = hit.transform.gameObject.;
这不是工作代码。你想要:
Vector3 blockPos = hit.transform.position;
我试图在我的 minecraft 克隆中制作脚本,但它一直返回此错误:
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
。此外,根据文档,GameObject
或 Vector3
上没有隐式转换运算符重载,将此问题留给一个简单的答案:
A GameObject
不是 Vector3
,不能明确地转换为一个。
有关您的问题的解决方案,请参阅 Fredrik 的回答。他似乎对 Unity3D 有所了解,而不仅仅是像我一样了解 .NET ;)。
如异常所述,您的代码有错误。您正在尝试将 GameObject 保存在 Vector3 中。
第 79 行:
Vector3 blockPos = hit.transform.gameObject.;
这不是工作代码。你想要:
Vector3 blockPos = hit.transform.position;