Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 LaGrangeEURL · Oct 28, 2021 at 02:49 PM · parentcomponentscardsdisabled

Component gets disabled when parent changed

Hello everyone ! I'm struggling with something I don't understand : I am working on a card game, in which all the cards are gameobjects instantiated in the deck. I have a Component called CardsPile that is responsible for holding the cards together, and it is the same for the deck and the hand.

When a card is drawn, it is moved from the deck to the hand. But for some reason, some scripts attached to the card get disabled. The funny thing is that it is not the case for the first card drawn during the game...

I'm kinda lost, so this is the code that draws the cards. Remove and Add are functions to "unparent" and "parent" the card from or to a CardsPile.

     public void DrawCombatCards(int cardsToDraw)
     {
         CardsPile hand = Hand.instance.hand;
         CardsPile combatDeck = CombatDeck.instance.combatDeck;
 
         for (int i = 0; i < cardsToDraw; i++)
         {
             GameObject card = combatDeck.Cards[combatDeck.Cards.Count - 1];
             combatDeck.Remove(card);
             hand.Add(card, 0, false);
         }
     }

This is the Add and Remove code :

     public float height = 0.5f;
     public float width = 1f;
     [Range(0f, 90f)] public float maxCardAngle = 5f;
     public float yPerCard = -0.005f;
     public float zDistance;
 
     public float moveDuration = 0.5f;
     public Transform cardHolderPrefab;
 
     readonly List<GameObject> cards = new List<GameObject>();
 
     public List<GameObject> Cards => new List<GameObject>(cards);
 
     public event Action<int> OnCountChanged;
 
     readonly List<Transform> cardsHolders = new List<Transform>();
 
     bool updatePositions;
     readonly List<GameObject> forceSetPosition = new List<GameObject>();
 
     public void Add(GameObject card, bool moveAnimation = true) => Add(card, -1, moveAnimation);
 
     public void Add(GameObject card, int index, bool moveAnimation = true)
     {
         Transform cardHolder = GetCardHolder();
 
         if (index == -1)
         {
             cards.Add(card);
             cardsHolders.Add(cardHolder);
         }
         else
         {
             cards.Insert(index, card);
             cardsHolders.Insert(index, cardHolder);
         }
 
         updatePositions = true;
 
         if (!moveAnimation)
             forceSetPosition.Add(card);
 
         OnCountChanged?.Invoke(cards.Count);
     }
 
     public void Remove(GameObject card)
     {
         if (!cards.Contains(card))
             return;
 
         Transform cardHolder = cardsHolders[cards.IndexOf(card)];
         cardsHolders.Remove(cardHolder);
         Destroy(cardHolder.gameObject);
 
         cards.Remove(card);
         card.transform.DOKill();
         card.transform.SetParent(null);
         updatePositions = true;
 
         OnCountChanged?.Invoke(cards.Count);
     }


Thank you all for reading ! Take care.

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
0
Best Answer

Answer by LaGrangeEURL · Oct 29, 2021 at 08:03 AM

Ok, I just solved it, but in my opinion this is a bug. Basically, when the parent gameObject was destroyed, every component attached to the child got disabled. So I had to move down the line responsible for destroying the parent. Voilà !

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

128 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

Related Questions

If i disable a GameObject does all it's components get disabled also? 2 Answers

Make a simple tree 1 Answer

GameObject parent use JavaScript 1 Answer

Need help, object parent object 1 Answer

Simulate 'Child' Effect by Script 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