- Home /
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[i].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.right*2, Quaternion.Euler(0, 0, 0)); myLife[i].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[i].SetActive(true); } } }
Thanks so much for all the help I can get :)
Why don't you try setting the renderer to inactive for the object you want 'removed'. That way it will not appear, you can still search for it and enable the renderer when you get to a checkpoint.
myLife[i].renderer.enabled = false;
If the gameObject has children you'll have to loop through all child renderer components and set them to inactive as well.
Thanks for the response, I tried that at first but since it deactivates only the renderer, the enemies and LD elements hit invisible parts of the ship
Answer by sfc.itzhak · Apr 28, 2013 at 06:04 PM
if i remamber correctly if you setactive off Gamobjects you cant find them under FindGameObjectsWithTag
look at http://docs.unity3d.com/Documentation/ScriptReference/GameObject.FindGameObjectsWithTag.html
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
I just realized that apprently we can't answer on our own question page since there's not the message that I wrote back then: even if I accepted your answer at the time, I wanted to say thank you, that helped me a lot in the game that I made ;)