Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Why do i Instantiate twice?

Discussion in 'Scripting' started by pKallv, Jun 21, 2014.

  1. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,122
    I am developing a card game were i flip the card when the players have the same. During this process I should instantiate a new object but for some reason it is instantiated twice? ...and i am not able to understand why this is happening two times.

    The code is for testing so it is not complete:

    Code (csharp):
    1.  
    2. voidOn2TimeTap(TapGesturegesture) {
    3. print ("===On2TimeTap=== xyz:" + xyz);>>> here I get "Instantiate xyz: 0"
    4. xyz++;
    5. //Flipobject - - - Makesurethattheclickisonappropriateobject
    6. if (gesture.Selection.tag != "MultipleIcon") {
    7.  
    8. FlipCardAndUpdatedMasterList(gesture.Selection);
    9.  
    And here is the actual code with the instantiate:

    Code (csharp):
    1.  
    2. //Flipcardanddoproperupdatesaswellasdestroyselectedprefab
    3. voidFlipCardAndUpdatedMasterList (GameObjectcardToFlip_GO) {
    4. print (" ===FlipCardAndUpdatedMasterList===");
    5. //Clonethepositionofthe "old" object
    6. Vector3pos_v3 = cardToFlip_GO.transform.position;
    7.  
    8.  
    9. //Checkifitisthebackorfrontofobject
    10. if (cardToFlip_GO.tag.LastIndexOf ("B") != -1) {
    11.  
    12.  
    13. } else { //faceisup
    14. //Loadabacksideprefabandsetcorrectposition
    15. newClone_GO = Resources.Load("BackSide") asGameObject;
    16.  
    17. //Findcorrecttagname
    18. convertedCardTag = CardsAndConversions.front2Back[cardToFlip_GO.tag];
    19. //Setnewnameandtag
    20. newClone_GO.name = convertedCardTag;
    21. newClone_GO.tag = convertedCardTag;
    22. }
    23.  
    24. //Removeflippedcardobjectfrommasterlist
    25. master_GameObject_List.Remove (cardToFlip_GO);
    26. //Addnewflippedcardobject
    27. master_GameObject_List.Add (newClone_GO);
    28. //Putnewobjectonthecorrectposition
    29. newClone_GO.transform.position = pos_v3;
    30. //Destroytheselectedobject
    31. Destroy(GameObject.FindWithTag(cardToFlip_GO.tag));
    32.  
    33. //Instantiatethenewupdatedobject
    34. Instantiate(newClone_GO);
    35. print ("Instantiate xyz: " + xyz); >>> here I get "Instantiate xyz: 1"
    36. xyz++;
    37.  
    38. }
    39.  
    I get 2 x "newClone_GO" instantiated but want only one.

    Would really appreciate some help to understand why!
     
    Last edited: Jun 21, 2014
  2. zaxvax

    zaxvax

    Joined:
    Jun 9, 2012
    Posts:
    220
    Well your code doesn't have any cycles and Instantiate is called only once. Probably your mistake is somewhere else, outside of this code. Maybe you have this script attached to 2 objects or something...

    Does it print "===On2TimeTap=== xyz:" 4 times or what is the situation with log?
     
  3. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,122
    hmm something is very strange with this code as the above is executed multiple times, i just do not understand why.
     
    Last edited: Jun 21, 2014
  4. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    I didn't read through your code. Just wanted to say if you use a Debug.Log() statement instead of print, the Unity Editor console shows a stacktrace of what is calling that function.
     
  5. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,122
    Thanks Garth.

    I am using FingerGestures and I use "voidOn2TimeTap(TapGesturegesture) {" setup for two taps and I do trigger this function, which trigger the Instantiation, twice. Maybe it is something with that plugin that is the reason for this.
     
  6. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,122
    I was able to solve the problem by renaming:

    Code (csharp):
    1.  
    2. void On2TimeTap (TapGesture gesture) {
    3.  
    to:
    Code (csharp):
    1.  
    2. void DoubleTap (TapGesture gesture)
    3.  
    It may be a bug in Fatal Frog's FingerGesture implementation as I have "On1TimeTap" for single tap and had "On2TimeTap" for double tap. The names is pretty similar but when i changed the problem is solved.