Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
-1
Question by phillipe · Nov 08, 2012 at 07:11 AM · gameobjectrandomchildrendeactivate

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 :)

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MarkFinn · Nov 08, 2012 at 08:10 AM 0
Share

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.

avatar image phillipe · Nov 09, 2012 at 04:17 AM 0
Share

@$$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.

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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;}
             }
         }
     }
 }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image phillipe · Nov 09, 2012 at 08:06 AM 0
Share

Thanks, will try these when I get home :)

avatar image phillipe · Nov 15, 2012 at 12:59 PM 0
Share

Sorry for the late reply, just tried the first script some days ago and it works! Cheers! Thanks again :)

avatar image Rock-Soft · Jul 03, 2019 at 09:01 PM 0
Share

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!

avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image phillipe · Nov 09, 2012 at 04:18 AM 0
Share

I'll try this, though I suck at program$$anonymous$$g. Thanks BTW :)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges