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 Giova507 · May 09, 2014 at 01:00 AM · componentchildrenaccessgetcomponentinchildrenhowto

How to access a Component from another Child when current is Destroyed

My Character(ParentObject) has 2 sprites/skins(dresses or whatever you wanna call it) that you can select from the menu(Children). Those 2 gameObjects(sprites) are stored in a list, whenever I select a skin from the menu, it Instantiate the selected sprite and destroys the current one(they Instantiate inside Character GameObject). Each Sprite has a Animator.

 public class Character : MonoBehaviour {
     bool move = false;
     Animator animator;
     
     void Start() {
     
     animator = transform.GetComponentInChildren<Animator>();
     
     }
  
 void Update(){
 if (Input.GetKeyDown (KeyCode.Space) || Input.GetMouseButtonDown (0)) {
 move = true;
                         
                         }
 
 }
 
     void FixedUpdate() {
     if (move == true) {
     
         animator.SetTrigger ("Moved");
     }

When the first sprite is destroyed, I get the error "The Object Type Animator has been destroyed but you are still trying to access it". I thought that if the first sprite was destroyed, the code (animator = transform.GetComponentInChildren();) would get the second Sprite Animator. But isn't working that way. How do I get the Animator from the second sprite when the first is destroyed.

P.D. : The 2 Sprites have NO script. Character Object has the script Character.

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 robertbu · May 09, 2014 at 01:06 AM 0
Share

Likely you will need to call this line again when a component is destroyed:

  animator = transform.GetComponentInChildren<Animator>();

Given the structure of your code, it may be simpler to just call it every frame.

1 Reply

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

Answer by Kiwasi · May 09, 2014 at 05:13 AM

The effect is part of the way the scripting language works. When you store the animator you store a reference to the specific instance of the animator component in memory, not the general animator on the childe object.

To fix you could simply grab a reference to the animator in the update loop

 void Update (){
     animator = transform.GetComponentInChildren<Animator>();
     //do other stuff with animator
 }

But getting a reference is expensive. This method will cost you in terms of performance.

A better way to do it as follows 1. First check if the animator component is not null. 2. If the animator component is null then get a new reference to the animator

 void Update (){
     if (!animator){
         animator = transform.GetComponentInChildren<Animator>();
     }
     //do other stuff with animator
 }

Its typically a good practice to check if a reference is null every time you refer to a object that might be destroyed.

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 Giova507 · May 09, 2014 at 05:45 AM 0
Share

Worked like a charm! You're the best 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

21 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

Related Questions

Changing a variable from one script to another C# 4 Answers

GetComponent in parent question 3 Answers

Access to variables from other script objects 3 Answers

Is it OK to use public accessers for unique gameobjects (C#) ? 3 Answers

How do I get components in grandchildren? 2 Answers


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