如何在实例化对象后面设置不同的属性?
How to set different properties behind instantiated objects?
我正在使用 Unity C# 制作游戏,其中在游戏开始时实例化了 10 个球体。当用户单击任何球体时,球体会使用 set active false 属性 隐藏。现在我想要的是在一些球体后面放置炸弹,这样当用户点击一个后面有炸弹的球体时,游戏就结束了。
球体
List<GameObject> hat;
float q, w, r;
public Spheres()
{
hat = new List<GameObject>();
q = -2.5f;w = -2.5f;r = -2.5f;
}
public void AddSphere(GameObject gola)
{
for (int i = 0; i < 5; i++)
{
GameObject abc = GameObject.Instantiate (gola);
abc.transform.position = new Vector3 (abc.transform.position.x + q, abc.transform.position.y, abc.transform.position.z + 1.5f);
hat.Add (abc); q += 1.15f;
}
}
public void HideSphere()
{
if(Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast (ray, out hit))
{
if (hit.transform.gameObject.CompareTag ("Sphere"))
{
hit.transform.gameObject.SetActive (false);
}
}
}
}
实例化球体
for (int i = 0; i < 5; i++)
{
GameObject abc = GameObject.Instantiate (gola);
abc.transform.position = new Vector3 (abc.transform.position.x + q, abc.transform.position.y, abc.transform.position.z + 1.5f);
hat.Add (abc); q += 1.15f;
}
你把你的炸弹放在多圆的地方children。
例如,当用户单击球体时,您检查 object 是否有一个名为 "bomb" 的 children。
void OnClick()
{
if (transform.Find("bomb" != null))
{
//gameover
}
else
{
gameobject.SetActive(false);
}
}
由于您还没有添加任何代码,我将只向您解释我将如何处理您的问题。我假设您已经为球体创建了一个 GameObject,它附加了一个脚本,该脚本停用 GameObeject 或使其在 onClick() 时不可见。所以现在要遵循的步骤是:
- 向名为
boolean hasBomb
的游戏对象添加一个布尔变量
- 当您在场景中实例化 10 个球体时,随机设置
布尔值 true 或 false
- 可选-如果你还想添加一个炸弹预制件,你可以
在具有炸弹的球体的相同位置实例化它们
属性设置为 true
- 在球体的
onClick()
方法中,您检查是否将 (hasBomb) 设置为
真
- 如果是这样,您可以加载一个名为 GameOver 的新场景,其中包含文本或菜单。使用
Application.LoadLevel("GameOver"
)
使用您在评论中提供的代码进行编辑:
List<GameObject> hat;
float q, w, r;
public Spheres() {
hat = new List<GameObject>();
q = -2.5f;
w = -2.5f;
r = -2.5f;
}
public void AddSphere(GameObject gola) {
for (int i = 0; i < 5; i++) {
GameObject abc = GameObject.Instantiate (gola);
abc.transform.position = new Vector3 (abc.transform.position.x + q, abc.transform.position.y, abc.transform.position.z + 1.5f);
hat.Add (abc);
q += 1.15f;
}
}
public void HideSphere() {
if(Input.GetMouseButtonDown(0)) {
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast (ray, out hit)) {
if (hit.transform.gameObject.CompareTag ("Sphere")) {
hit.transform.gameObject.SetActive (false);
}
}
}
}
我假设 gola 是您在编辑器中创建的游戏对象。因此,与其将该游戏对象标记为 "sphere" 以便稍后检测您是否使用 Physics.Raycast 按钮按下它,您可以做的是创建一个名为 hideSphere.cs 的脚本。在该脚本中,您应该添加:
public boolean hasBomb;
还有
function OnMouseDown()
{
if(hasBomb)
Application.LoadLevel("GameOver")
renderer.enabled = false;
//Or
//Destroy(gameObject);
}
那么你应该:
- 向 gola 游戏对象添加碰撞器(因此它会检测点击)
- 将脚本hideSphere.cs附加到gola游戏对象
编辑 2:
现在随机决定球体是否有炸弹:
GameObject abc = GameObject.Instantiate (gola);
abc.transform.position = new Vector3 (abc.transform.position.x + q, abc.transform.position.y, abc.transform.position.z + 1.5f);
//Here we decide if it has a Bomb or not
float chance = Random.Range(0.0f, 1.0f);
if(chance > 0.75f)
abc.GetComponent.<hideSphere>().hasBomb = true;
hat.Add (abc);
注意:通过行 abc.GetComponent.<hideSphere>().hasBomb = true;
,您可以访问变量 hasBomb,它是脚本 hideSphere.cs 中的一个 public 变量,它附加到 GameObject gola。但在这种情况下,您只能为当前迭代中生成的特定实例修改该属性。
我正在使用 Unity C# 制作游戏,其中在游戏开始时实例化了 10 个球体。当用户单击任何球体时,球体会使用 set active false 属性 隐藏。现在我想要的是在一些球体后面放置炸弹,这样当用户点击一个后面有炸弹的球体时,游戏就结束了。
球体
List<GameObject> hat;
float q, w, r;
public Spheres()
{
hat = new List<GameObject>();
q = -2.5f;w = -2.5f;r = -2.5f;
}
public void AddSphere(GameObject gola)
{
for (int i = 0; i < 5; i++)
{
GameObject abc = GameObject.Instantiate (gola);
abc.transform.position = new Vector3 (abc.transform.position.x + q, abc.transform.position.y, abc.transform.position.z + 1.5f);
hat.Add (abc); q += 1.15f;
}
}
public void HideSphere()
{
if(Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast (ray, out hit))
{
if (hit.transform.gameObject.CompareTag ("Sphere"))
{
hit.transform.gameObject.SetActive (false);
}
}
}
}
实例化球体
for (int i = 0; i < 5; i++)
{
GameObject abc = GameObject.Instantiate (gola);
abc.transform.position = new Vector3 (abc.transform.position.x + q, abc.transform.position.y, abc.transform.position.z + 1.5f);
hat.Add (abc); q += 1.15f;
}
你把你的炸弹放在多圆的地方children。 例如,当用户单击球体时,您检查 object 是否有一个名为 "bomb" 的 children。
void OnClick()
{
if (transform.Find("bomb" != null))
{
//gameover
}
else
{
gameobject.SetActive(false);
}
}
由于您还没有添加任何代码,我将只向您解释我将如何处理您的问题。我假设您已经为球体创建了一个 GameObject,它附加了一个脚本,该脚本停用 GameObeject 或使其在 onClick() 时不可见。所以现在要遵循的步骤是:
- 向名为
boolean hasBomb
的游戏对象添加一个布尔变量
- 当您在场景中实例化 10 个球体时,随机设置 布尔值 true 或 false
- 可选-如果你还想添加一个炸弹预制件,你可以 在具有炸弹的球体的相同位置实例化它们 属性设置为 true
- 在球体的
onClick()
方法中,您检查是否将 (hasBomb) 设置为 真 - 如果是这样,您可以加载一个名为 GameOver 的新场景,其中包含文本或菜单。使用
Application.LoadLevel("GameOver"
)
使用您在评论中提供的代码进行编辑:
List<GameObject> hat;
float q, w, r;
public Spheres() {
hat = new List<GameObject>();
q = -2.5f;
w = -2.5f;
r = -2.5f;
}
public void AddSphere(GameObject gola) {
for (int i = 0; i < 5; i++) {
GameObject abc = GameObject.Instantiate (gola);
abc.transform.position = new Vector3 (abc.transform.position.x + q, abc.transform.position.y, abc.transform.position.z + 1.5f);
hat.Add (abc);
q += 1.15f;
}
}
public void HideSphere() {
if(Input.GetMouseButtonDown(0)) {
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast (ray, out hit)) {
if (hit.transform.gameObject.CompareTag ("Sphere")) {
hit.transform.gameObject.SetActive (false);
}
}
}
}
我假设 gola 是您在编辑器中创建的游戏对象。因此,与其将该游戏对象标记为 "sphere" 以便稍后检测您是否使用 Physics.Raycast 按钮按下它,您可以做的是创建一个名为 hideSphere.cs 的脚本。在该脚本中,您应该添加:
public boolean hasBomb;
还有
function OnMouseDown()
{
if(hasBomb)
Application.LoadLevel("GameOver")
renderer.enabled = false;
//Or
//Destroy(gameObject);
}
那么你应该:
- 向 gola 游戏对象添加碰撞器(因此它会检测点击)
- 将脚本hideSphere.cs附加到gola游戏对象
编辑 2:
现在随机决定球体是否有炸弹:
GameObject abc = GameObject.Instantiate (gola);
abc.transform.position = new Vector3 (abc.transform.position.x + q, abc.transform.position.y, abc.transform.position.z + 1.5f);
//Here we decide if it has a Bomb or not
float chance = Random.Range(0.0f, 1.0f);
if(chance > 0.75f)
abc.GetComponent.<hideSphere>().hasBomb = true;
hat.Add (abc);
注意:通过行 abc.GetComponent.<hideSphere>().hasBomb = true;
,您可以访问变量 hasBomb,它是脚本 hideSphere.cs 中的一个 public 变量,它附加到 GameObject gola。但在这种情况下,您只能为当前迭代中生成的特定实例修改该属性。