嘿伙计们,我正在制作类似Terraria的游戏,我一直在创建生物群系但是在渲染块时遇到了麻烦。我已将第二个代码放入" GameMaster"游戏中的游戏对象。并在第二个代码中使用Gameobject Chunk将游戏对象附加到第一个代码。出于某种原因,我的GetComponent<>()。width;没有认识到ChunkGenerator类。因此给我错误。一切都有帮助。谢谢。我已经附上了以下两个代码。
Here's the first code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ChunkGenerator : MonoBehaviour {
public GameObject DirtTile;
public GameObject StoneTile;
public GameObject GrassTile;
public GameObject SandTile;
public int heightAddition;
public int width;
public float heightMultiplier;
float worldSeed;
float playerSpawnBiome;
public float smoothness;
public float mountSmooth;
void Start () {
// playerSpawnBiome = Random.Range (-5f, 10f);
// if (playerSpawnBiome > 0f) {
// GenerateMountainBiome ();
// } else if ((playerSpawnBiome > 0f) && (playerSpawnBiome <= 5f)) {
GeneratePlainsBiome ();
// } else {
// GenerateSandBiome ();
// }
}
public void GeneratePlainsBiome() {
worldSeed = Random.Range (-10000f, 10000f);
for (int i = 0; i < width; i++)
{
int h = Mathf.RoundToInt(Mathf.PerlinNoise (worldSeed, (i + transform.position.x / smoothness) * heightMultiplier) + heightAddition);
for (int j = 0; j < h; j++) {
GameObject selectedTile;
if (j < h - 3) {
selectedTile = StoneTile;
} else if (j < h - 1) {
selectedTile = DirtTile;
} else {
selectedTile = GrassTile;
}
GameObject newTile = Instantiate (selectedTile, Vector3.zero, Quaternion.identity) as GameObject;
newTile.transform.parent = this.gameObject.transform;
newTile.transform.localPosition = new Vector3 (i, j);
}
}
}
public void GenerateSandBiome() {
worldSeed = Random.Range (-10000f, 10000f);
for (int i = 0; i < width; i++)
{
int h = Mathf.RoundToInt(Mathf.PerlinNoise (worldSeed, (i / smoothness) * heightMultiplier) + heightAddition);
for (int j = 0; j < h; j++) {
GameObject selectedTile;
if (j < h - 3) {
selectedTile = StoneTile;
} else if (j < h - 1) {
selectedTile = SandTile;
} else {
selectedTile = SandTile;
}
Instantiate (selectedTile, new Vector3(i, j), Quaternion.identity);
}
}
}
public void GenerateMountainBiome() {
worldSeed = Random.Range (-10000f, 10000f);
for (int i = 0; i < width; i++)
{
int h = Mathf.RoundToInt(Mathf.PerlinNoise (worldSeed, (i / 3f) * mountSmooth) * heightMultiplier) + heightAddition;
for (int j = 0; j < h; j++) {
GameObject selectedTile;
if (j < h - 3) {
selectedTile = StoneTile;
} else if (j < h - 1) {
selectedTile = DirtTile;
} else {
selectedTile = GrassTile;
}
Instantiate (selectedTile, new Vector3(i, j), Quaternion.identity);
}
}
}
}
这是第二个代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ChunkGenerators : MonoBehaviour {
public GameObject chunk;
int chunkWidth;
public int numChunks;
float seed;
void Start() {
chunkWidth = chunk.GetComponent<ChunkGenerator> ().width;
seed = Random.Range (-1000000f, 1000000f);
Generate ();
}
public void Generate() {
int lastX = -chunkWidth;
for (int i = 0; i < numChunks; i++) {
GameObject newChunk = Instantiate (chunk, new Vector3 (lastX + chunkWidth, 0f), Quaternion.identity) as GameObject;
newChunk.GetComponent<ChunkGenerator> ().seed = seed;
lastX += chunkWidth;
}
}
}
答案 0 :(得分:0)
因为你引用了错误的脚本。您错误地引用了ChunkGenerator
而不是ChunkGenerators
。
更改:强>
newChunk.GetComponent<ChunkGenerator> ().seed = seed;
以强>
newChunk.GetComponent<ChunkGenerators> ().seed = seed;