How to deactivate gameObjects instead of destroying them ?

Hi everyone!

I’m kind of new in Unity and I’m trying to create a little shoot’em up with the following idea: the ship is composed of 20 elements, and when the player presses Space the ship shoots an element (to kill an enemy, destroy something, etc). What I’m doing (or rather trying to do!) is deactivate an element of the ship and instantiate the same prefab ‘shot’ each time the player presses Space.

The problem that I encounter is that the elements are destroyed when the player shoots, and not deactivated like I want them to be (so that they can be part of the ship again later when the ship reaches checkpoints or something). I precise in the Update ‘myLife*.SetActive(false);’ and I see them being deactivated in the hierarchy, yet in the array in the inspector they’re being destroyed. I don’t understand…*
(And it also instantiates two prefabShots at each shot instead of one, maybe it is part of the problem as well or it comes from something else ?)
Here’s what my code looks like (put on the ship):
`
var myLife : GameObject[];

  • // reference to the prefab blueprint for shots*

  • var prefabShot:Transform;*

  • // our starting x position*

  • private var originalX:float = 0.0;*

  • function Awake()*

  • {*

  • // set our starting x position*

  • originalX = transform.position.x;*

  • }*

function Update ()
{

**if(Input.GetButtonDown(“Shoot”))
{
for(var i:int = 0; i < myLife.length ;i–)
{

  •   myLife = GameObject.FindGameObjectsWithTag("Element").OrderBy(function (go) go.name).ToArray();*
    

_ Instantiate(prefabShot, transform.position + Vector3.right2, Quaternion.Euler(0, 0, 0));_
_myLife
.SetActive(false);*_
}**

}

// get axis input
* var moveY:float = Input.GetAxis(“Vertical”);*
* var moveX:float = Input.GetAxis(“Horizontal”);*

* // apply force depending on input*
_ rigidbody.AddForce(Vector3.up * moveY * moveForce * Time.deltaTime);
rigidbody.AddForce(Vector3.right * moveX * moveForce * Time.deltaTime);_

* // make sure our z value never changes*
* transform.position.z = 0.0;*

}

function OnTriggerEnter(other : Collider)
{
* if (other.tag == “Checkpoint”)*
* {*
for(var i:int = 0; i < myLife.length; i++)

* {*
_ myLife*.SetActive(true);
}
}
}
`*
Thanks so much for all the help I can get :slight_smile:_

if i remamber correctly if you setactive off Gamobjects you cant find them under FindGameObjectsWithTag

look at Unity - Scripting API: GameObject.FindGameObjectsWithTag

Returns a list of activeGameObjects tagged tag. Returns null if no GameObject was found.

so when you say destroyed you mean you get a result of 0

SFC