[英]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
。 另外,根据文档,在GameObject
或Vector3
上没有隐式的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.