- Home /
Why does instantiation of clone cause duplicates
I have a clone that is supposed to be instantiated when conditions are met, but for some reason, when they ARE met. The clone appears more than once simultaneously. Its not constant. as some times it is only one, and other times it is four or five. Debugging through didn't give me an answer. so I'm lost
You should post your code for meeting the conditions and for instantiating, so we can check what's wrong.
sorry, this code deals with the instantiation of the unlocked item
public BuyableProperties ItemUnlocked() {
// item not initialized or item already unlocked
if (unlockableItem == null || !unlockableItem.isLocked)
{
return null;
}
foreach (TrophyProperties trophy in trophies)
{
if (trophy.isLocked)
{
// returns null if any trophy is still locked
return null;
}
}
// FIX$$anonymous$$E - not sure if this should be instantiating new object or getting an instance somehow...
return (BuyableProperties)Instantiate(unlockableItem);
}
Answer by Demigiant · Nov 08, 2011 at 07:46 PM
I'd need to see more code, but I assume that the ItemUnlocked method is called more than once for some reason.
First thing that comes to my mind is that you're calling it when a buttons is pressed, but you're using GetButton (which repeats while the button is pressed) instead of GetButtonDown (which is sent only the first frame the button is pressed).
Have you tried adding a trace to see how many times this method is accessed?
Its not a GetButton
if (trophyProp.inventoryCount == 1)
{
GameObject collectionGameObj = GameObject.Find(mysteryBox.trophyInBox.belongsToCollection);
if (collectionGameObj != null)
{
Debug.Log("Spawning unlock item");
BuyableProperties unlockedItem = collectionGameObj.GetComponent<CollectionProperties>().ItemUnlocked();
if (unlockedItem)
{
StartCoroutine(DelayedUnlockSpawn(1.0f, unlockedItem, pos));
}
}
}
this is what calls the unlocked item
Your answer
Follow this Question
Related Questions
How to add multiple clones of an object to a list ? Currently only 1 clone gets added. 1 Answer
Why are multiple objects being instantiated when I use clone, but not regular instantiation? 1 Answer
Cloning Problem Script 0 Answers
How can I get the x position for the left(and right) of the screen? 2 Answers
How do I make a clone of a prefab appear on the correct layer? [5.2.2f1] 1 Answer