- Home /
Find the solution somewhere else
Ground cycle destroy,Ground destruction problem
Hi, I'm making a roll a ball game to find my level cuz I didn't used unity for a long.
I made a 15x15 ground separated in 15x15 (225) cubes.
I want one of them get selected every 5 sec then color it red for 0.5 sec and then destroy it.
My problem is that I want the cubes to disapear one by one evry 5 sec and they all disapear in a few seconds.
Youtube video showing what's happening.
Here's my script:
#pragma strict
var RN : int = -1;
var groundPart : GameObject[];
var destroyCooldown : int = 5;
var timer : float = 0;
var groundIsDestroyed : boolean = true;
function Start () {
groundPart = GameObject.FindGameObjectsWithTag("Ground");
}
function Update () {
if(groundIsDestroyed == true) {
Cooldown();
}
}
function Cooldown () {
yield WaitForSeconds(destroyCooldown);
groundIsDestroyed = false;
DestroyGround();
}
function DestroyGround () {
if(groundIsDestroyed == false) {
RN = Random.Range(0, groundPart.length);
if(RN <= groundPart.length) {
groundPart[RN].GetComponent(MeshRenderer).material.color = Color.red;
yield WaitForSeconds(0.5);
Destroy(groundPart[RN]);
}
RN = -1;
groundIsDestroyed = true;
}
}
Thank you for ur help.
It disappear because it was destroyed, try replace the destroy with SetActive(bool value).
It don't change anything...
$$anonymous$$y problem is that I want the cubes to disapear one by one evry 5 sec and they all disapear in a few seconds.
every 5 sec or every half sec (0.5)? if need change the color at 0.5 and destroy at 5 then divide the code in 2 coroutines one for color change and other for destroy.
If you need change the color and after 5 seconds destroy, after 0.5 repeat the process you will need 2 WaitForSeconds in your code one of 5 after change the color and other of 0.5 before the end of loop.
Try it:
function Start () {
groundPart = GameObject.FindGameObjectsWithTag("Ground");
StartCoroutine(DestroyGround());
}
function DestroyGround () {
while(true){
RN = Random.Range(0, groundPart.length);
if(RN ]]]
I'm sorry but I still have a problem, it work perfectly but when groundPart.length reach 0 the unity freeze and do not responce. I want to know how I shoul use StopCoroutine(xxx()); this don't work:
function Update () {
if(groundPart.Length == 0) {
StopCoroutine(DestroyGround());
}
}
try use:
Coroutine corr;
corr= StartCoroutine(DestroyGround());
StopCoroutine(corr);
Answer by doublemax · Sep 10, 2016 at 02:58 PM
WaitForSeconds() etc. only work in Coroutines(). You need to call those functions with StartCoroutine().