- Home /
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 :)
How sure are you that there will always be exactly 3 children? Are the names of the children consistent? Do you have a reference to the children in code anywhere? Can such references be made in the editor?
If you can answer these, I can bash out a script for it in about 5 $$anonymous$$utes.
@$$anonymous$$arkFinn Say for example, I create a box then duplicates it 3 times (same name, now 4 boxes). I placed them in different locations in the scene, while in the hierarchy I attached all of them to an empty game object. Once the level starts, 3 of these boxes will be deactivated randomly. I want the script to be attached to the empty game object.
Answer by MarkFinn · Nov 09, 2012 at 05:21 AM
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[i].gameObject.active=true;}
else{itemsForRandomEnable[i].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<GameObject> itemsForRandomEnable=new List<GameObject>();
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[i].gameObject.active=true;}
else{itemsForRandomEnable[i].gameObject.active=false;}
}
}
}
}
Sorry for the late reply, just tried the first script some days ago and it works! Cheers! Thanks again :)
First one worked out the box for me. Set the amount you want in the inspector, then drag each deactivated game object you want to randomize.
Thank you! Huge time saver!
Answer by FakeBerenger · Nov 08, 2012 at 08:26 AM
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.
I'll try this, though I suck at program$$anonymous$$g. Thanks BTW :)
Your answer
Follow this Question
Related Questions
Deactivate the children 2 Answers
Referencing children of an instantiated object. 1 Answer
how can I display a variable as a GUIText 5 Answers
Deactivate an object - and all scripts in that object deactivated? 1 Answer
Is it possible to get the width (in unity units) of a GameObject including all of its children? 1 Answer