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 carnivoris · Dec 23, 2014 at 06:26 PM · gameobjectsetactive

SetActive() not working on child objects

I'm trying to enable/disable weapon models on my player model when he picks up a new weapon. This is the code that doesn't seem to work.

 void exchangeWeapon(int newWeapon){
 
         if (newWeapon == 1) {
             Debug.Log ("Changing weapons to "+m4a1_equip.gameObject.name);
             ak47_equip.gameObject.SetActive(false);
             m4a1_equip.gameObject.SetActive(true);
 
             ak47_loot.gameObject.SetActive(true);
             m4a1_loot.gameObject.SetActive(false);
         }
         if (newWeapon == 2) {
             Debug.Log ("Changing weapons to "+ak47_equip.gameObject.name);
             ak47_equip.gameObject.SetActive(true);
             m4a1_equip.gameObject.SetActive(false);
 
             ak47_loot.gameObject.SetActive(false);
             m4a1_loot.gameObject.SetActive(true);
         }
         if (newWeapon == 0) {
             ak47_equip.gameObject.SetActive(false);
             m4a1_equip.gameObject.SetActive(false);
         }
     }

There are no errors when this code is executed. I'm disabling the model on the ground and enabling the model in the player's hands. The Debug.Logs execute properly, so I know the code is being run and the gameObject is being read correctly. The model on the ground disappears/reappears properly. The gun model in the player's hands doesn't appear, though.

Without errors, I'm a bit lost as to what I've done wrong.

Comment
Add comment
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

2 Replies

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

Answer by Owen-Reynolds · Dec 23, 2014 at 06:53 PM

In general, setActive on child objects works fine. I toggle them on/off, mostly like you're doing, all the time.

Pause the game and check the Inspector. Focus on one gun (sometimes they both don't work, but for different reasons.)

Look at the "held" nug. Is it set active? If so, it will still be inactive if any parents are -- maybe an empty parent is inactive. Click everything to make sure it can be visible. Unchild it (if needed) and double-click to it. Maybe it's visible but 50 meters above you by mistake.

If it's not active, make the variable public (stop and run it again) and click to find it. Maybe m4a1_equip is also set to the ground object. Check the loot's are set up correctly (and not aimed at the held guns by mistake.)

If it's all hooked-up correctly and still not being made active, maybe code elsewhere is messing it up. Comment out a bunch of lines, put the SetActive's in Start if needed. Check for other SetActives.

Comment
Add comment · Show 6 · 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 carnivoris · Dec 23, 2014 at 08:16 PM 0
Share

It seems that I'm having trouble finding the child gameObjects I'm looking for. I see the error this time. ak47_equip and m4a1_equip are deeply nested child objects of the player character (They're childed to the right hand).

 void Start () {
         ak47_loot = GameObject.Find ("ak47");
         m4a1_loot = GameObject.Find ("$$anonymous$$4A1 Sopmod");
 
         ak47_equip = transform.FindDeepChild ("ak47_equip").gameObject;
         m4a1_equip = transform.FindDeepChild ("$$anonymous$$4A1_equip").gameObject;
 ...
 }
 

This is the code I'm using to find those game objects. I've got a class called FindDeepChild in my project.

 public static class TransformDeepChildExtension
 {
     //Breadth-first search
     public static Transform FindDeepChild(this Transform aParent, string aName)
     {
         var result = aParent.Find(aName);
         if (result != null)
             return result;
         foreach(Transform child in aParent)
         {
             result = child.FindDeepChild(aName);
             if (result != null)
                 return result;
         }
         return null;
     }

It's been fairly confusing trying to figure out how to find and activate those guns. I feel like it's something simple or maybe I'm just doing it wrong.

avatar image Owen-Reynolds · Dec 24, 2014 at 01:16 AM 0
Share

Are you finding the wrong ak47_equip? If you weren't finding anything, I think SetActive would error-out.

FindDeepChild seems fine(*). But you can look directly: transform.Find("body/rArm/hand/gun1");. Or you can make them public vars and drag them in pre-game. You might find the problem that way, them convert back to DeepChild after.

(*)It says breath-first, but it's really depth-first. Just saying.

avatar image carnivoris · Dec 24, 2014 at 05:04 AM 0
Share

There's only one ak47_equip object in the scene. I'll see if I can look directly at it. I didn't know that was possible. That would alleviate a lot of headache.

avatar image carnivoris · Jan 06, 2015 at 05:57 PM 0
Share

I've got the code finding the objects, but the objects on the character don't actually enable. I know the code reaches the point where they SHOULD be enabled

 void exchangeWeapon(int newWeapon){
 
         if (newWeapon == 1) {
             Debug.Log ("Changing weapons to "+m4a1_equip.gameObject.name);
             ak47_equip.gameObject.SetActive(false);
             m4a1_equip.gameObject.SetActive(true);
 
             ak47_loot.gameObject.SetActive(true);
             m4a1_loot.gameObject.SetActive(false);
         }
         if (newWeapon == 2) {
             Debug.Log ("Changing weapons to "+ak47_equip.gameObject.name);
             ak47_equip.gameObject.SetActive(true);
             m4a1_equip.gameObject.SetActive(false);
 
             ak47_loot.gameObject.SetActive(false);
             m4a1_loot.gameObject.SetActive(true);
         }
         if (newWeapon == 0) {
             ak47_equip.gameObject.SetActive(false);
             m4a1_equip.gameObject.SetActive(false);
         }
     }

The Debug.Log lines execute, the _loot objects appear/disappear properly, but the _equip weapons still don't appear. There are no errors in the console, either. Any suggestions?

avatar image carnivoris · Jan 07, 2015 at 05:12 PM 1
Share

I finally got it. I moved the Find lines to find the _equip weapons to the exchangeWeapon method and it works now. Not sure what that's all about, but it's working. I might have to find another $$anonymous$$4 model, though... when I equip this one, my frames go to absolute hell. But, that's another issue :)

Show more comments
avatar image
0

Answer by wieryeveska · Nov 29, 2016 at 08:17 AM

In my case... check if scale, width, height... etc of any parent or parent's parent is not 0 while playing check any other condition of rect transform is properly set while playing

If any one of them is not properly set then it wouldn't work properly too. Parent object must be set enough with the settings in order to process whats in the children

Comment
Add comment · 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

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

28 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 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

How to swap gameObjects on the press of a button? 1 Answer

game object set active on trigger enter? 3 Answers

How do I alter all children of a gameobject at once? 1 Answer

Override SetActive on a GameObject? 1 Answer

activate GameObject dont work 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