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
0
Question by iamananimal · Apr 28, 2013 at 05:30 PM · javascriptarraygameobjectsactivatedeactivate

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

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 Jazzer008 · Apr 28, 2013 at 08:37 PM 0
Share

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.

avatar image iamananimal · Apr 29, 2013 at 08:34 AM 0
Share

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

1 Reply

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

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

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 iamananimal · May 31, 2013 at 04:06 PM 0
Share

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

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

15 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 avatar image avatar image avatar image

Related Questions

Deactivated Targets in Shooting Game Cause Score to Never Stop Adding One 1 Answer

How to Deactivate Gameobjects Array in Unity 4? 1 Answer

Cannot deactivate / activate gameobject, even in editor! 6 Answers

Activate/deactivate all GameObjects with same tag c# 1 Answer

Need help using GUI.Button/ input storage 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