Multiple objects deleted after one another
I have 6 cubes in the game that I want to deactivate after one another in a random order. When the code runs everything waits for 5 seconds like its supposed to but once the first cube is deleted all the rest go with it. Each of the objects is tagged "target" and the array list has 6 values. So the only issue should be in the IEnumerator logic
Here is the code:
public Text displayMessage;
public GameObject[] targets;
void Start()
{
if (targets.Length == 0)
{
targets = GameObject.FindGameObjectsWithTag("target");
}
}
// Update is called once per frame
void Update () {
displayMessage.text = ("BOx score" + userInfo.SessionBoxScore);
StartCoroutine(deactivateObj(targets[1]));
}
private IEnumerator deactivateObj(GameObject obj)
{
int ran = Random.Range(0, targets.Length);
obj = targets[ran];
yield return new WaitForSeconds(5);
if (targets[ran].activeSelf)
{
print("deactivated" + obj.name);
obj.SetActive(false);
}
//Destroy(obj);
}
Comment
You have destroy(obj) out of the if function, making it destroy no matter what. $$anonymous$$aybe place it in a if statement ?
Your answer
![](https://koobas.hobune.stream/wayback/20220612112242im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Help me!!! Error not receiving image Unity3D. Please 0 Answers
"No Android Device Found" on Build and Run 1 Answer
Drag - Rotate 0 Answers