Randomly deactivate game objects

Hey there! Anyone know about a script to attach a game object and randomly kill/deactivate its 3 children (out of 4), leaving only 1 child when the game starts? I’ve been searching for this but can’t find anything. I’m fine with links, too. Thanks :slight_smile:

Here you go.
This will work with a more generic approach. You can assign objects which are childed to the gameobject it is attached to, or an other objects you like in the scene.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class RandomEnabledOnStart : MonoBehaviour {
	
	public List<GameObject> itemsForRandomEnable=new List<GameObject>();

	void Start () {
		if (itemsForRandomEnable!=null && itemsForRandomEnable.Count>0)
		{
			int itemId=new System.Random().Next(itemsForRandomEnable.Count);
			for (int i=0; i<itemsForRandomEnable.Count;i++)
			{
				if (i==itemId){itemsForRandomEnable*.gameObject.active=true;}*

_ else{itemsForRandomEnable*.gameObject.active=false;}_
_
}_
_
}_
_
}_
_
}*_
if you are sure you really want to use the childed objects approach then this variant will do just that.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class RandomEnabledOnStart : MonoBehaviour {

* void Start () {*
* List itemsForRandomEnable=new List();*
* foreach (Transform child in transform)*
* {*
* itemsForRandomEnable.Add(child.gameObject);*
* }*
* if (itemsForRandomEnable!=null && itemsForRandomEnable.Count>0)*
* {*
* int itemId=new System.Random().Next(itemsForRandomEnable.Count);*
* for (int i=0; i<itemsForRandomEnable.Count;i++)*
* {*
_ if (i==itemId){itemsForRandomEnable*.gameObject.active=true;}
else{itemsForRandomEnable.gameObject.active=false;}
}
}
}
}*_

You need to use the function Random.Range, and the number of children transform.childCount. Note that the range function with int is [a,b[, so you don’t need to substract one to the child count.