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 Animan968 · Sep 08, 2014 at 05:03 AM · childrensetactivecycle

How do you enable/disable a child object individually? C#

I want to be able to enable/disable child objects individually using (possibly) gameObject.SetActive(false); and gameObject.SetActive(true); in Unity 4. I want to be able to cycle through a couple of game objects that are children of a larger object. The goal is to have a spaceship with multiple guns that are in one spot on the ship. Pressing tab (for example) would cycle through, say, three different weapons. Is there a simple way to do this? Each gun would have its own particle effects, physics, animations and sounds that would need to be disabled/enabled as well. Thanks in advance.

Comment
Add comment · Show 1
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 Jeric-Miana · Sep 08, 2014 at 06:12 AM 0
Share
 var changeWeapon : boolean = true;
 var weapon1 : GameObject;
 var weapon2 : GameObject;
 
 function Update()
 {
     changeWeapon = !changeWeapon;
     if(Input.Get$$anonymous$$eyDown("tab"))
     {
     if(changeWeapon){
     weapon1.SetActive(true);
     weapon2.SetActive(false);
     } else {
     weapon1.SetActive(false);
     weapon2.SetActive(true);
     }
     }
 }

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by orb · Sep 08, 2014 at 06:43 AM

I think the answer is a little more advanced than simply setting all objects in a hierarchy inactive. When you use the SetActive() method you also toggle the visibility of the game object - it doesn't seem right to me having guns disappear!

Here's how I'd do it:

  1. One game object on the ship needs to act as the parent of the guns

  2. Each gun object has a component class, for example ShipGun

  3. Each ShipGun has a boolean that holds whether it can be fired and a pointer to an "on" state particle effect attached to it

  4. Finally each ShipGun should have its own on/off method(s) which toggle "canFire" (or whatever) and use SetActive(canFire) on its particle effect

You should get the list of guns with GetComponentsInChildren() and refresh it only if the guns are changed. Then you can rotate the active one by walking through this list. Pseudo-code:

 ShipGun[] guns = gunMount.GetComponentsInChildren<ShipGun>();
 int activeGun = 1;
 for(int i=0;i<guns.Length;i++)
 {
     if(i == activeGun)
     {
         guns[i].ToggleState(true);
         guns[i].canFire = true;
     } else {
         guns[i].ToggleState(false);
         guns[i].canFire = false;
     }
 }
 
Comment
Add comment · Show 2 · 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 Animan968 · Sep 08, 2014 at 11:51 PM 0
Share

$$anonymous$$aking the Guns disappear is exactly what i want. The objective(maybe I wasn't clear, I apologize)is to be able to switch what weapons are attached (and active) to the ship before a battle. Im very new to Unity, so bear with me.To sum it up, by pressing the left and right arrows, you switch between three weapons (wep1, wep2, wep3 for example). Each of these three weapons attaches to the ship in the same spot, so you can only equip and activate one at a time. How would that be coded?

avatar image orb · Sep 09, 2014 at 07:46 PM 0
Share

(I posted a reply earlier, but it seems to be missing in action!)

$$anonymous$$ake an integer variable activeGun, and increase it with each press of TAB. Set it to zero if it goes past the max number of weapons. Also make a public variable for the particle effect on each gun and stick it there.

Now the loop should contain something like this:

 if(i == activeGun)
 {
     guns[i].SetActive(true);
     guns[i].particle.SetActive(true);
 } else {
     guns[i].SetActive(false);
     guns[i].particle.SetActive(false);
 }

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to access children in script attached to gameObject 1 Answer

Load Children One at a Time 1 Answer

Foreach loop trying to grab all children in scene 1 Answer

Particular Child Object Refuses to setActive - Others work fine 0 Answers

physics.OverlapSphere colliders 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