Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 xthunderduckx · Apr 02, 2019 at 08:20 PM · scripting problemchild objectparent-child

How to modify child objects with parent script?

In my FPS game, I'm trying to make weapons cease rendering when the player isn't using weapon[weaponNum]. Though I don't have an actual array, I have an empty game object with a script on it that checks each frame whether or not the player is trying to switch weapons. When the player selects a weapon, I want the previous weapon to dissapear/become invisible, and for the weapon of weaponNum to become visible.


How would I reference those gameObjects in code? For example, if the player presses 1, I want their currently equipped weapon to dissapear, and the weapon in slot 1 to reappear. I'm using weaponNum for a script called useWeaps(int fireType) as well with a switch statement that fires a different projectiles depending on what weaponNum is.

     void weaponSelect()
     {
         //change weapon model, and equipment number.  use different function for firing, reloading, etc depending on which number is selected.  
         if (Input.GetKey(KeyCode.Alpha1))
         {
             weaponNum = 1;
             //deactivateCurrent() activateNum() model change
         }
         else if (Input.GetKey(KeyCode.Alpha2))
         {
             weaponNum = 2;
             //deactivateCurrent() activateNum() model change
         }
     }


alt textTo illustrate what I mean, here's an image. I would want gun1 to become invisible when the user has gun2 equipped, the opposite with gun1 equipped.
The script is on the weaponModels Parent, so how does one reference and control the child objects within that parent?

gun12.png (10.5 kB)
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

1 Reply

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

Answer by highpockets · Apr 02, 2019 at 09:15 PM

The script on the parent can reference other game objects by doing the following:

 public GameObject gunOne;
 public GameObject gunTwo;
 

Now in the inspector, you can drag gun1 and gun2 into the respective fields on the script/component. If you prefer to keep them private for whatever reason, you can do the following:

 [SerializedField] GameObject gunOne;
 [SerializedField] GameObject gunTwo;

The above keeps the fields visible in the inspector so you can add the game objects my dragging and dropping, but now they are private. You can also assign them from code:

 GameObject gunOne;
 GameObject gunTwo;
 
 void Start(){
 
 gunOne = GameObject.Find(“gun1”); //you have to have the exact name as the parameter/string.
 gunTwo = GameObject.Find(“gun2”);
 }

Now since you are using the parent script to reference them, you can activate or deactivate them or move the one you don’t want off screen somewhere. To activate/deactivate:

 gunOne.SetActive(false);
 gunTwo.SetActive(true);

Or move the one you don’t want to another position:

 gunOne.transform.position = new Vector3(5000,5000,5000);

If you move it off screen somewhere and it remains a child, it will continue to be active and will respond to fire for example unless you set a bool or have states to know if it is in use or not.

Hope that helps, cheers

Comment
Add comment · Show 3 · 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 xthunderduckx · Apr 02, 2019 at 09:19 PM 0
Share

I've been trying to avoid anything public variable wise just to keep in practice though if this is the only way it'll work, I just don't like working with strings and memory in this way with my experience in C++. A many times it caused issues using strings and public stuff like that.

Also, doesn't .Find have to search every single game object in the scene? That seems extremely inefficient; even if it only has to run once that is still a LOT of stuff to look through.

avatar image highpockets xthunderduckx · Apr 02, 2019 at 09:36 PM 0
Share

Use [SerializedField]. That keeps it private and makes it available in the inspector. Yes, Find() is not super efficient, but if you just have to run it once at the start of the game, not too big of a deal. Not sure how many objects you have though.

avatar image highpockets xthunderduckx · Apr 02, 2019 at 09:52 PM 0
Share

I forgot to mention that you can use tags as well. You can tag the object in the inspector (“guns” for example). And then use FindGameObjectsWithTag() or you can look up object of type as well with FindObjectsOfType().. Lots of ways to skin a cat. Hopefully that helps. If you think I’ve answered the question, please mark it as answered though.

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

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

Related Questions

Moving certain sprites in my animation causes weird slingshot effect 0 Answers

Finding an objects children when they don't have any scripts attached? 1 Answer

Modifying HingJoint2D in a child GameObject 0 Answers

How to create a child object of an object with a specific tag? 1 Answer

Extremely confused with parent and child object system 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