如何制作游戏对象的副本?
我试图在游戏中做的是,当球销毁盒子时,它的随机数是为了产生不同的功率,我做了随机的chanses,但我不知道如何在被摧毁的块上产生供电图标。当它产生它时,它只是漂浮着,Yiu需要用您用来baunce baunce的平台女巫来接他。 我的盒子上的代码是:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class destroy : MonoBehaviour
{
private string BALL_TAG = "ball";
public AudioClip coin;
public AudioSource src;
private float digit;
public GameObject spawnTo;
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag(BALL_TAG))
{
movement PBDM = FindObjectOfType<movement>();
PBDM.Destroy();
src.clip = coin;
src.Play();
digit = Random.Range(0, 101);
if (digit <= 15 && digit >= 11)
print("THERE IS A PLACE WHERE I NEED TO SPAWN POWER UP ON XYZ OF BOX.");
else if (digit <= 10 && digit >= 1)
Debug.Log("epic");
else
Debug.Log("basic");
Destroy(gameObject);
}
}
}
此调试我仅用于测试它是否有效。
What I am trying to do in my game is when the ball destroy box its makes random number for spawning different power ups I did random chanses but I don't know how to spawn power up icon on place of destroyed block. And when its spawned its just floating down and yiu need to pick him up by touching him with platform witch you are using to baunce the ball.
My code on boxes is:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class destroy : MonoBehaviour
{
private string BALL_TAG = "ball";
public AudioClip coin;
public AudioSource src;
private float digit;
public GameObject spawnTo;
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag(BALL_TAG))
{
movement PBDM = FindObjectOfType<movement>();
PBDM.Destroy();
src.clip = coin;
src.Play();
digit = Random.Range(0, 101);
if (digit <= 15 && digit >= 11)
print("THERE IS A PLACE WHERE I NEED TO SPAWN POWER UP ON XYZ OF BOX.");
else if (digit <= 10 && digit >= 1)
Debug.Log("epic");
else
Debug.Log("basic");
Destroy(gameObject);
}
}
}
This Debugs I used just for testing if it workes.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因此,如果我正确理解您,您只想 instantiate 对象。
要在场景上创建一个新的GameObject,您可以按照以下步骤进行操作:
创建要产生的GameObject的预制。您可以通过将GameObject从层次结构拖到资产窗口来做到这一点。
drag&amp;将其放在要产生预制的对象上的C#脚本组件上。
将您的if语句更改为:
您可以另外阅读有关 instantiate()如果您想将GameObject实例化为其他GameObject的孩子。
So if I understand you correctly you want just to Instantiate object.
To create a new GameObject on the scene you can follow these steps:
Create a Prefab of the GameObject that you want to spawn. You can do it by dragging the GameObject from the hierarchy to the assets window.
Drag & drop it on C# Script component on the object you want to spawn the prefab.
change your if statement to:
You can additionally read about the Instantiate() method if you want to instantiate GameObject as a child of some another GameObject.