How to reactivate a gameObject?

Once I set transform.gameObject.active = false, I can’t set it back to transform.gameObject.active = true. Why?

I’ve tried making a different function, if loops, while loops, coroutine, and nothing works.

U cannot reactivate it ! The reason is once u set the game object’s enable to false, the script and other components stops working, u cannot reactivate it from the same script or any of the scripts that is attached to that particular game object…

you should have the reference to the game object outside somewhere and then u can control the enable as u like! I hope it helps :slight_smile:

Just take it back one step...

Make an empty GameObject and put the object you want to enable/disable grouped as a child.

Take the script you were using on the now child object and put it on the parent.

You can declare a GameObject var in this script, and attach the child to it in the Inspector. Then, enable/disable this child object, instead of just using `gameObject.active = false`.

This way, your invisible parent object is always active to control the state of the actual object you want to toggle.

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?