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 RandomNewbie · Apr 28, 2020 at 04:18 AM · destroycard

I think Unity is respawning GameObjects on it's own

So I'm making a card game. When the player clicks his card and then the deck, the card should get discarded and replaced.

So I have this:

 public GameObject slot1; //item previously selected, ie the card
 
 void Deck() {
         if (!slot1.GetComponent<K_Selectable>().usable)
         {
             game.SortOnes(slot1);
             game.DealCard(slot1);
             Destroy(slot1);
         }
     }
 
 void Card(GameObject selected)
     {
         if (slot1 = this.gameObject) { //Selects the card u click
             slot1 = selected;
         }
 }

And I have this as well

 public void SortOnes(GameObject slot1) {
         for (int i = 0; i < 4; i++)
         {
             if (p1Pos[i].transform.GetChild(0).gameObject == slot1)
             {
                 
                 p1s[i].Add(deck.Last<string>());
                 deck.RemoveAt(deck.Count - 1);
                 print("Cards left: " + deck.Count);
                 position = i;
                 print(position);
             }
         }
     }
 
     public void DealCard(GameObject slot1) {
         foreach (string card in p1s[position])
         {
             GameObject newCard = Instantiate(cardPrefab, new Vector3(slot1.transform.position.x, slot1.transform.position.y, slot1.transform.position.z), Quaternion.identity, p1Pos[position].transform);
             newCard.name = card;
             newCard.GetComponent<K_Selectable>().faceUp = true;
         }
     }
 
     

But the problem is: Although a new card is added, the previous one is NOT destroyed, but if I run just the Destroy(); command, it works; which makes me think Unity is respawning it for some reason.

Any help is appreciated

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

3 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by icehex · Apr 28, 2020 at 04:42 AM

As I understand, Unity won't remove the object until you Destroy() it, by design. When you Instantiate() a new one, it simply creates a brand new object. If you're overriding a GameObject variable with the new GameObject, it's only overriding the reference that points from the old object to a reference that points to the new object, while the old object continues to exist until destroyed. So make sure to Destroy your object before using the same variable to start pointing to a new instantiated object.

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 RandomNewbie · Apr 28, 2020 at 05:49 PM 0
Share

I (think I) understand the answer, so I tried this (hope this was what you meant): I removed the Destroy(); from the Deck(); and ins$$anonymous$$d changed my DealCard(); method to this: public void DealCard(GameObject slot1) { foreach (string card in p1s[position]) { float posx = slot1.transform.position.x; float posy = slot1.transform.position.y; float posz = slot1.transform.position.z; Destroy(slot1); GameObject newCard = Instantiate(cardPrefab, new Vector3(posx, posy, posz), Quaternion.identity, p1Pos[position].transform); newCard.name = card; newCard.GetComponent().faceUp = true; }

But it doesn't seem to work either. I really don't know what to do now

avatar image icehex RandomNewbie · Apr 28, 2020 at 10:09 PM 0
Share

So, you need the slot1 GameObject transform's position in order to instantiate NewCard. The code you had before seems like it does that, and you said if you just run Destroy() on it, it works. if it works, is there still undesirable behavior?


Also, unless you need anything other than slot1's position, I would make DealCard be:

      public void DealCard(Vector3 position) {
          foreach (string card in p1s[position])
          {
              GameObject newCard = Instantiate(cardPrefab, position, Quaternion.identity, p1Pos[position].transform);
              newCard.name = card;
              newCard.GetComponent<K_Selectable>().faceUp = true;
          }
      }
  

And to call DealCard,

 game.DealCard(slot1.transform.position);

avatar image RandomNewbie · Apr 30, 2020 at 02:21 AM 0
Share

Thanks, I think I found the root of the problem (the way I have configured my card deck) so I'll probably just start over, considering I have barely done anything. Thanks a lot tho, even though it didn't quite work for this scenario, your feedback and advice will surely become usefull in the future, I'll remove the question tomorrow

avatar image icehex RandomNewbie · Apr 30, 2020 at 05:59 AM 0
Share

Post the rest of the code that's not in here, I might be able to point you in the right direction at least. I couldn't see the issue with the code you posted above

avatar image RandomNewbie icehex · Apr 30, 2020 at 08:10 PM 0
Share

I can send u the whole project if u want. I was working on the Kalashnikov one, but you'll find also a Solitaire (the tutorial I followed and then try to modify), but there was an underlying problem with my apporach (u need lists for Solitaire, but not for Kalashnikov...). So my problem starts there. If you want to try to better understand what I was trying to do, check https://www.youtube.com/watch?v=IiRk-yGfAjc&t=4s

I was trying to achieve the card swap mechanic. I can't send the Project here, bc of size limit

Show more comments
avatar image
0

Answer by mehtanitish · Apr 28, 2020 at 07:22 PM

Try using DestroyImmediate() instead of Destroy(). https://docs.unity3d.com/ScriptReference/Object.DestroyImmediate.html?_ga=2.222371737.309142467.1588099273-234286291.1569601227

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 RandomNewbie · Apr 28, 2020 at 08:00 PM 0
Share

Already tried, doesn't seem to work either

avatar image
0

Answer by Cassos · Apr 28, 2020 at 09:01 PM

Your code is really confusing me. I really want to help you, but your way to ask the question doesn't really help.

Where should it be destroyed? I propably could write this completly new. I propably will.

Comment
Add comment · Show 2 · 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 Cassos · Apr 28, 2020 at 09:07 PM 0
Share

@RandomNewbie I think I figured out what was the problem:

     void Deck()
     {
         if (!slot1.GetComponent<K_Selectable>().usable)
         {
             GameObject oldCard = slot1;
             game.SortOnes(slot1);
             game.DealCard(slot1);
             Destroy(oldCard);
         }
     }

Based on the information you provided, I think that you replaced the old GameObject of the previous card reference with the new one so it can't be deleted. Shat should fix it. Based on the code you provided I can't help you more because I have to know the whole code to debug it.

Greetings,

$$anonymous$$ax

avatar image RandomNewbie · Apr 28, 2020 at 10:20 PM 0
Share

Thanks dude, I'm not really that good at C#, as I have more experience with Java, but not for games... I didn't really know what was enough for my question to be answered properly.

I tried what you said, but it did the same thing it was doing before (video attached)link text. Tell me if I should provide anything else for it to be easier for ppl to answer me, I'm really stuck.

Btw, it seems that it's not changing the selected nor the slot1 object. Using print(); it seems that slot1 is always the same as oldCard, even after the Instancing... Srry for all this, it's my first game and I think I bit more than I could chew...

unity-error.zip (462.9 kB)

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

133 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

Related Questions

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

Destroy game object with tag 1 Answer

Trash Collection For Instantiated Game Object 1 Answer

How Can I Make My Fire Effect Disappear After I Shoot Object? 1 Answer

I want to destroy this gameobject when this gameobject is destroyed. 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