同一个对象在 C# 中一次又一次地被实例化
The Same Object is Getting Instantiated again and again in C#
我是编码初学者。几天来,我一直在尝试重新制作流行的手机游戏 'Crossy Road'。当我尝试创建关卡生成脚本时,我 运行 遇到了问题。我面临的问题是,当我尝试 运行domly 实例化 3 个不同的对象时,(在本例中为草、路和水)只生成草。如果有人能告诉我为什么它一直重复实例化 'Grass' 对象,我将不胜感激。我正在 Unity 5 中重新创建游戏。代码如下——
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;
}
}
}
}
}
}
}
如果有人能告诉我错误,我将不胜感激。
谢谢!
您已将其他对象的代码放置在 firstRand 为 1 时调用的代码组中。(FirstRand ==2) 和 (FirstRand ==3) 在此位置永远不会为真,您写的是无法到达的代码。
您的声明 if (firstRand == 2)
永远不会到达,因为它包含在 if (firstRand ==1)
声明中。
您需要像这样构建您的 if
语句:
if (firstRand == 1)
{
...
}
if (firstRand == 2)
{
...
}
if (firstRand == 3)
{
...
}
如果您想改进代码,请执行以下操作:
firstRand = Random.Range(1,4);
secondRand = Random.Range(1,8);
GameObject instance = null;
for (int i = 0; i < secondRand; i++)
{
intPos = new Vector3(0, 0, distPlayer);
distPlayer += 1;
switch (firstRand)
{
case 1:
instance = Instantiate(Grass) as GameObject;
break;
case 2:
instance = Instantiate(Road) as GameObject;
break;
case 3:
instance = Instantiate(Water) as GameObject;
break;
}
instance.transform.position = intPos;
}
我是编码初学者。几天来,我一直在尝试重新制作流行的手机游戏 'Crossy Road'。当我尝试创建关卡生成脚本时,我 运行 遇到了问题。我面临的问题是,当我尝试 运行domly 实例化 3 个不同的对象时,(在本例中为草、路和水)只生成草。如果有人能告诉我为什么它一直重复实例化 'Grass' 对象,我将不胜感激。我正在 Unity 5 中重新创建游戏。代码如下——
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;
}
}
}
}
}
}
}
如果有人能告诉我错误,我将不胜感激。 谢谢!
您已将其他对象的代码放置在 firstRand 为 1 时调用的代码组中。(FirstRand ==2) 和 (FirstRand ==3) 在此位置永远不会为真,您写的是无法到达的代码。
您的声明 if (firstRand == 2)
永远不会到达,因为它包含在 if (firstRand ==1)
声明中。
您需要像这样构建您的 if
语句:
if (firstRand == 1)
{
...
}
if (firstRand == 2)
{
...
}
if (firstRand == 3)
{
...
}
如果您想改进代码,请执行以下操作:
firstRand = Random.Range(1,4);
secondRand = Random.Range(1,8);
GameObject instance = null;
for (int i = 0; i < secondRand; i++)
{
intPos = new Vector3(0, 0, distPlayer);
distPlayer += 1;
switch (firstRand)
{
case 1:
instance = Instantiate(Grass) as GameObject;
break;
case 2:
instance = Instantiate(Road) as GameObject;
break;
case 3:
instance = Instantiate(Water) as GameObject;
break;
}
instance.transform.position = intPos;
}