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 ToastandBananas · Jun 22, 2018 at 12:57 AM · unity 5unity 2dspritesenableenable and disable script

Can't Get .enabled to Work for Children Sprites

So I have an object with a few children. I want to be able to disable/enable 2 of them via one of my scripts. I feel like I'm doing things right, but apparently I'm not. And yes, my game objects have the appropriate tags that are being searched for in my code. I'll paste the relevant bits of code below:

 public bool isAiming = false;
 Animator playerAnim;
 
 GameObject arm;
 GameObject weapon;
 
 SpriteRenderer armRenderer;
 SpriteRenderer weaponRenderer;
 
 void Start()
     {
         playerAnim = gameObject.GetComponent<Animator>();
 
         arm = GameObject.FindGameObjectWithTag("Arm");
         weapon = GameObject.FindGameObjectWithTag("EquippedWeapon");
 
         armRenderer = arm.GetComponent<SpriteRenderer>();
         weaponRenderer = weapon.GetComponent<SpriteRenderer>();
     }
 
 void Update()
     {
         CheckIfAiming();
     }
 
 void CheckIfAiming() 
     {
         if (Input.GetButton("Fire2")) // Holding right click
         {
             isAiming = true;
 
             armRenderer.enabled = true;
             weaponRenderer.enabled = true;
 
             playerAnim.SetBool("isAiming", isAiming);
         }
         else
         {
             isAiming = false;
 
             armRenderer.enabled = false;
             weaponRenderer.enabled = false;
 
             playerAnim.SetBool("isAiming", isAiming);
         }
     }

What gives? I want to just enable the sprite rendering when I hold down the right mouse click. SetActive(false/true) is not an option as I still want code to affect them while they're not visible.

Am I just trying to access the SpriteRenderers incorrectly?

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 ToastandBananas · Jun 22, 2018 at 04:16 PM

Finally figured it out!

Here's what I used:

 Renderer[] renderChildren = GetComponentsInChildren<Renderer>();
 
 int i;
 for(i = 1; i < renderChildren.Length; ++i)
 {
     renderChildren[i].renderer.enabled = true;
 }

And then when I wanted the SpriteRenderers to be disabled I did the same thing, except I set it to false. I started i equal to 1 so that it would skip the parent object's renderer and only change 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
avatar image
0

Answer by LeeroyLin · Jun 22, 2018 at 04:09 AM

You mean your weapon is an object has many children? If you want to show or hide children of that object, you have to enable or disable the SpriteRenderer component on every child. So what you do is to use a loop like this:

 foreach (Transform child in weapon.transform){
     child.GetComponent<SpriteRenderer>().enabled = false;
 }

I'm not sure if this is what you mean, hope this can help you. Any question can reply, good day.

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 ToastandBananas · Jun 22, 2018 at 04:17 PM 0
Share

Nope, the weapon and the arms are the children. The characters body is the parent. I figured it out though, thanks!

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

What are the best practices when importing 2D pixel sprites 1 Answer

How do I change Raycast's direction based on movement input? 1 Answer

How do I select a group full of sprites, between two points? 1 Answer

Unity Game View won't refresh my sprites 1 Answer

Teleport Not Working in Unity 2D 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