私はコーディングの初心者です。私は数日間、人気のモバイルゲーム「Crossy Road」を再作成しようとしていました。私はLevel Generationスクリプトを作成しようとしていましたが、問題が発生しました。私が直面している問題は、3つのオブジェクトをランダムにインスタンス化しようとすると(この場合はGrass、Road and Water)、草だけが生成されるということです。なぜ誰かが 'Grass'オブジェクトを何度もインスタンス化し続ける理由を教えてもらえれば、とても感謝しています。私はUnity 5 fyiでゲームを作り直しています。次のようにコードがある -同じオブジェクトがC#で何度もインスタンス化されています
using UnityEngine;
using System.Collections;
public class LevelGenerationScript : MonoBehaviour {
public GameObject Water;
public GameObject Road;
public GameObject Grass;
int firstRand;
int secondRand;
int distPlayer = 10;
Vector3 intPos = new Vector3(0,0,0);
void Update()
{
if (Input.GetButtonDown("up"))
{
firstRand = Random.Range(1,4);
if(firstRand == 1)
{
secondRand = Random.Range(1,8);
for(int i = 0;i < secondRand; i++)
{
intPos = new Vector3(0,0,distPlayer);
distPlayer += 1;
GameObject GrassIns = Instantiate(Grass) as GameObject;
GrassIns.transform.position = intPos;
}
if(firstRand == 2)
{
secondRand = Random.Range(1,8);
for(int i = 0;i < secondRand; i++)
{
intPos = new Vector3(0,0,distPlayer);
distPlayer += 1;
GameObject RoadIns = Instantiate(Road) as GameObject;
RoadIns.transform.position = intPos;
}
if(firstRand == 3)
{
secondRand = Random.Range(1,8);
for(int i = 0;i < secondRand; i++)
{
intPos = new Vector3(0,0,distPlayer);
distPlayer += 1;
GameObject WaterIns = Instantiate(Water) as GameObject;
WaterIns.transform.position = intPos;
}
}
}
}
}
}
}
誰かが私に間違いを伝えることができれば、私はそれを本当に感謝します。 ありがとう!
最初の場所で 'if(firstRand == ...'がネストされていない、つまり最初の 'if'を'} 'で閉じ、次に' if'などを実行してください。 –