C# 当我销毁我的对象时,在不同的位置创建新对象
当我点击我的鼠标按钮并销毁游戏对象时,我想在随机位置创建一个新的游戏对象,我尝试了Instatitate和其他方法,但没有成功。有人能帮我吗C# 当我销毁我的对象时,在不同的位置创建新对象,c#,visual-studio,unity3d,C#,Visual Studio,Unity3d,当我点击我的鼠标按钮并销毁游戏对象时,我想在随机位置创建一个新的游戏对象,我尝试了Instatitate和其他方法,但没有成功。有人能帮我吗 public GameObject tapObject; private float respawnTime = 1f; public float xMin; public float xMax; public float yMin; public float yMax; void Start() { StartCoroutine(sp
public GameObject tapObject;
private float respawnTime = 1f;
public float xMin;
public float xMax;
public float yMin;
public float yMax;
void Start()
{
StartCoroutine(spawnEnemyTime());
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
Destroy(tapObject);
}
}
private void RandomSpawnObject()
{
tapObject.transform.position = new Vector2(Random.Range(xMin, xMax), Random.Range(yMin, yMax));
}
IEnumerator spawnEnemyTime()
{
while (true)
{
yield return new WaitForSeconds(respawnTime);
RandomSpawnObject();
}
}
如果你想保留同一个游戏对象,你可以避免破坏它,相反,你可以控制它是否被破坏。应该是这样的: 编辑: 使用GameObject.SetActive() 使用GameObject.Instantiate()
请注意,在使用GameObject.Instantiate()时,您需要附加一个预置。如果您想保留同一个游戏对象,您可以避免破坏它,而可以控制它是否被破坏。应该是这样的: 编辑: 使用GameObject.SetActive() 使用GameObject.Instantiate()
请注意,在使用GameObject.Instantiate()时,需要附加一个预置。解决问题的简单方法是创建一个方法并使用计时器调用它,在该方法中只需使用以下代码 代码 矢量3位置=新矢量3(随机范围(-10.0f,10.0f),0,随机范围(-10.0f,10.0f)); 实例化(预置、位置、四元数标识) 注
您可以使用您在应用程序中使用的游戏对象,而不是使用预设使用。解决问题的简单方法是创建一个方法并使用计时器调用它,在该方法中只需使用以下代码 代码 矢量3位置=新矢量3(随机范围(-10.0f,10.0f),0,随机范围(-10.0f,10.0f)); 实例化(预置、位置、四元数标识) 注
你可以使用你在应用程序中使用的游戏对象,而不是使用预设使用这不起作用,他不激活游戏对象,但1秒钟后他没有再次激活,你会改为设置位置吗,然后将其设置为活动?我想停用对象设置新位置,然后再次激活我尝试设置活动不工作instatiate alsoDid你尝试我的代码@Stefanmocilovic,因为当我尝试时,你能在instagram上跟我来,给我发个信息解释我的情况吗?lord_stefan_m我的instagram,给我发个信息这没用,他没激活游戏对象,但一秒钟后他就没激活了你会改为设置位置吗,然后将其设置为活动?我想停用对象设置新位置,然后再次激活我尝试设置活动不工作instatiate alsoDid你尝试我的代码@Stefanmocilovic,因为当我尝试时,它满足了你的需要。你能在instagram上跟我来,给我发一条消息解释我的情况吗?lord_stefan_m我的instagram,并给我发消息
public GameObject tapObject;
private float respawnTime = 1f;
public float xMin;
public float xMax;
public float yMin;
public float yMax;
void Start()
{
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
tapObject.SetActive(false);
StartCoroutine(spawnEnemyTime());
}
}
private void RandomSpawnObject()
{
tapObject.SetActive(true);
tapObject.transform.position = new Vector2(Random.Range(xMin, xMax), Random.Range(yMin, yMax));
}
IEnumerator spawnEnemyTime()
{
yield return new WaitForSeconds(respawnTime);
RandomSpawnObject();
}
public GameObject prefab;
public GameObject tapObject;
private float respawnTime = 1f;
public float xMin;
public float xMax;
public float yMin;
public float yMax;
void Start()
{
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
Destroy(tapObject);
StartCoroutine(spawnEnemyTime());
}
}
private void RandomSpawnObject()
{
tapObject = GameObject.Instantiate(prefab, new Vector2(Random.Range(xMin, xMax), Random.Range(yMin, yMax)), Quaternion.identity);
}
IEnumerator spawnEnemyTime()
{
yield return new WaitForSeconds(respawnTime);
RandomSpawnObject();
}