x


Activate children

Okay I have pretty much what I needed done so far, and Now I just need to know what the complete script is to activate or deactivate the children from a game object. If you know please tell me I have only 2 weeks left to get this project DONE!

more ▼

asked Mar 29, 2010 at 09:52 AM

Trevor 1 gravatar image

Trevor 1
61 22 22 30

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

There's a built-in function in the Unity API to do this for you:

GameObject.SetActiveRecursively

(note, this activates/deactivates the children and the object itself - i.e. not just the children)

more ▼

answered Mar 29, 2010 at 10:52 AM

duck gravatar image

duck ♦♦
48.1k 135 256 495

How is this done in Unity 4 now that SetActiveRecursively is depreciated?

Oct 23, 2013 at 06:17 PM Giantbean

gameObject.SetActive(false) is the new version, which disables the gameobject + all of its children

Oct 23, 2013 at 06:42 PM Basez64

Yes Basez64 that will deactivate everything but the reverse gameObject.SetActive(true) is not reactivating.

Rutter expalined in another post how to loop through and reactivate the children.

 GameObject MyObjName = GameObject.Find("ObjName");
                        foreach (Transform child in MyObjName.transform)
                     {
                             child.gameObject.SetActive(true);
                     } 

However I cant get this to work. (May be an NGUI issue)

Edit-- Yep NGUI uses:

NGUITools.SetActiveChildren(MyObjName, true);

It would have been nice if they had made this clear to its users, otherwise its a great tool.

Oct 23, 2013 at 08:47 PM Giantbean
(comments are locked)
10|3000 characters needed characters left

I did it that way and it works but my child does't move anymore.

Here's a code that disable my additional enemy on start and reactivate it when player has 200 points.

 using UnityEngine;
 using System.Collections;
  
 public class EnemyActivatorController2 : MonoBehaviour {
  
     // Use this for initialization
     void Start () {
         GameObject ActiveEnemy2 = GameObject.Find ("EnemyActivator2");
         foreach (Transform child in ActiveEnemy2.transform) {
                         child.gameObject.SetActive (false);
                 }
  
  
     }
  
     // Update is called once per frame
     void Update () {
         if (PlayerController.Points == 200) {
             GameObject ActiveEnemy2 = GameObject.Find ("EnemyActivator2");
             foreach (Transform child in ActiveEnemy2.transform) {
                 child.gameObject.SetActive (true);
             } 
         }
  
     }
 }

It working but my additional enemy spawn and doesn't move anymore.

Here is enemy script:

using UnityEngine; using System.Collections;

public class EnemyController2 : MonoBehaviour {

 public float speed = -1;
 private Transform spawnPoint;
 
 // Use this for initialization
 void Start () {
     spawnPoint = GameObject.Find("SpawnPoint").transform;
     rigidbody2D.velocity = new Vector2 (speed, 0);
 
 
 
 }
 
 // Update is called once per frame
 void Update () {
 
 }
 
 void OnBecameInvisible()
 {
     if (Camera.main == null)
                     return;
 
     float yMax = Camera.main.orthographicSize - 0.5f;
     transform.position = new Vector3( spawnPoint.position.x,
                                      Random.Range (-yMax, yMax),
                                      transform.position.z );
 }
 

}

Any ide how to make he move again?

more ▼

answered Aug 29 at 07:26 PM

Imagineum gravatar image

Imagineum
16 2

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x9403
x302

asked: Mar 29, 2010 at 09:52 AM

Seen: 9390 times

Last Updated: Aug 29 at 07:26 PM