- Home /
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
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.
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
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);
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
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
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
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
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.
@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
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...
Your answer
Follow this Question
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