Unity 5 无法获取组件(尝试从另一个 class 中获取组件)
Unity 5 Can't Get Component (Trying to pull from another class)
大家好,我正在制作一款类似 Terraria 的游戏,我一直在创建生物群落,但在渲染块时遇到了问题。我已将第二个代码放入游戏中的 "GameMaster" 游戏对象中。并在第二个代码中使用 Gameobject Chunk 将一个游戏对象附加到第一个代码中。出于某种原因,我的 GetComponent<>().width;无法识别 ChunkGenerator class。因此给我错误。什么都有帮助。谢谢。我附上了下面的两个代码。
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;
}
}
}
这是因为您引用了错误的脚本。您错误地引用了 ChunkGenerator
而不是 ChunkGenerators
.
变化:
newChunk.GetComponent<ChunkGenerator> ().seed = seed;
到
newChunk.GetComponent<ChunkGenerators> ().seed = seed;
大家好,我正在制作一款类似 Terraria 的游戏,我一直在创建生物群落,但在渲染块时遇到了问题。我已将第二个代码放入游戏中的 "GameMaster" 游戏对象中。并在第二个代码中使用 Gameobject Chunk 将一个游戏对象附加到第一个代码中。出于某种原因,我的 GetComponent<>().width;无法识别 ChunkGenerator class。因此给我错误。什么都有帮助。谢谢。我附上了下面的两个代码。
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;
}
}
}
这是因为您引用了错误的脚本。您错误地引用了 ChunkGenerator
而不是 ChunkGenerators
.
变化:
newChunk.GetComponent<ChunkGenerator> ().seed = seed;
到
newChunk.GetComponent<ChunkGenerators> ().seed = seed;