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

[C] Code problem, can't figure out the problem

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

  1. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,122
    Hi,
    I have spent hours to try to understand what I am doing wrong but just can't figure it out and would like to ask for some help.

    This is what i am trying to achieve:

    I have two objects placed like this:

    I double-click on the H2 and the following happens:



    if I double-click on the object with it's back (H2B), second object, it goes back to the first situation, which is correct.

    However, when I double-click the first time (H2) I want the object to flip (back up) and be on top, which it does not do (THIS IS THE PROBLEM). I have done this before but at this time I cannot figure out what I am doing wrong when going from front to back as it works correctly going from back to front?

    Here is the code i use:

    Code (csharp):
    1.  
    2. void SingleTap(TapGesturegesture) {
    3.     print ("SingleTap " + singleNr);
    4.     singleNr++; // For testing
    5.     if (gesture.Selection != null) {
    6.         print (">>>>Single tap gesture.Selection != null");
    7.         selected_GameObject = gesture.Selection;
    8.  
    9.         DoTheLineCast();
    10.         RefreshSortOrderAndZandPutObjectOnTop (selected_GameObject);
    11.      }
    12.     print ("Single Tap end");
    13.  }
    14.  
    15. //=======================================================================================//
    16. //======================================DoubleTap========================================//
    17. //=======================================================================================//
    18.  
    19. void DoubleTap(TapGesturegesture) {
    20.     print ("DoubleTap " + doubleNr);
    21.     doubleNr++; / for testing
    22.     if (gesture.Selection.tag != "MultipleIcon") { //Donotprocessiftheclickisontheicon
    23.  
    24.         isThisDoubleClick = true;
    25.  
    26.         //selected_GameObject = gesture.Selection;
    27.  
    28.         print ("Double: " + gesture.Selection); //selected_GameObject.tag);
    29.  
    30.         DoTheLineCast();
    31.  
    32.         FlipCardAndUpdatedMasterList(selected_GameObject);
    33.         print ("Efter: " + selected_GameObject.tag);
    34.         //Updatemaster_GameObject_List
    35.         RefreshSortOrderAndZandPutObjectOnTop (selected_GameObject);
    36.  
    37.      }
    38.  
    39.  }
    40.  
    41. //=======================================================================================//
    42. //===========================FlipCardAndUpdatedMasterList================================//
    43. //=======================================================================================//
    44.  
    45. //Flipcardanddoproperupdatesaswellasdestroyselectedprefab
    46. void FlipCardAndUpdatedMasterList (GameObjectcardToFlip_GO) {
    47.     print ("FlipCardAndUpdatedMasterList");
    48.     //Clone the position of the "old" object
    49.     Vector3 pos_v3 = cardToFlip_GO.transform.position;
    50.  
    51.     //Check if it is the back or front of object
    52.     if (cardToFlip_GO.tag.LastIndexOf ("B") != -1) { //Back is up
    53.  
    54.         //Find correct tag name
    55.         convertedCardTag = CardsAndConversions.back2Front[cardToFlip_GO.tag];
    56.  
    57.         //Load a back side prefab and set correct position
    58.         temp_GameObject = Resources.Load(convertedCardTag) asGameObject;
    59.  
    60.      } else { //face is up
    61.  
    62.         //Find correct tag name
    63.         convertedCardTag = CardsAndConversions.front2Back[cardToFlip_GO.tag];
    64.  
    65.         //Load a back side prefab and set correct position
    66.         temp_GameObject = Resources.Load("BackSide") asGameObject;
    67.  
    68.         //Set new name and tag
    69.         temp_GameObject.name = convertedCardTag;
    70.         temp_GameObject.tag = convertedCardTag;
    71.      }
    72.  
    73.     //Remove flipped card object from masterlist
    74.     master_GameObject_List.Remove (cardToFlip_GO);
    75.     //Add new flipped object
    76.     master_GameObject_List.Add (temp_GameObject);
    77.     //Put new object on the correct position
    78.     temp_GameObject.transform.position = pos_v3;
    79.     //Destroy the selected object
    80.     Destroy(GameObject.FindWithTag(cardToFlip_GO.tag));
    81.  
    82.     //Instantiate the new updated object
    83.     Instantiate(temp_GameObject);
    84.  
    85.     //Update master_GameObject_List
    86.     temp_GameObject = GameObject.FindWithTag (convertedCardTag);
    87.     RefreshSortOrderAndZandPutObjectOnTop (selected_GameObject);
    88.  
    89.     selected_GameObject = temp_GameObject; //GameObject.FindWithTag (convertedCardTag);
    90.  
    91.  }
    92.  
    93. //=======================================================================================//
    94. //==================================DoTheLineCast========================================//
    95. //=======================================================================================//
    96.  
    97. void DoTheLineCast() {
    98.     print ("DoTheLineCast " + castNr);
    99.     castNr++; // testing
    100.     //When selecting an object move it to the front and refresh z and SortOrder for
    101.         //all objects
    102.     LayerMask theLayer;
    103.     theLayer = (1 << LayerMask.NameToLayer("Card")); //set the layer to be clickable
    104.     Vector2 clickedPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    105.  
    106.     //Get current world space position of mouse cursor
    107.     RaycastHit2D[] hits = Physics2D.LinecastAll(clickedPos,clickedPos,theLayer);
    108.  
    109.     int nrOfHits = hits.Length;
    110.     print ("#hits: " + nrOfHits);
    111.  
    112.     //Manage exception error
    113.     try {
    114.         selected_GameObject = GameObject.FindWithTag (hits [nrOfHits - 1].collider.tag);
    115.      }
    116.     catch {
    117.         print ("EXCEPTION START " + selected_GameObject.tag);
    118.         return;
    119.      }
    120.  
    121.     //Update master_GameObject_List
    122.     RefreshSortOrderAndZandPutObjectOnTop (selected_GameObject);
    123.  
    124.  }
    125.  
     
  2. zaxvax

    zaxvax

    Joined:
    Jun 9, 2012
    Posts:
    220
    1. temp_GameObject.transform.position = pos_v3;
    2. //Destroy the selected object
    3. Destroy(GameObject.FindWithTag(cardToFlip_GO.tag));
    4. //Instantiate the new updated object
    5. Instantiate(temp_GameObject);
    I don't like this part. You really need to set transform.position at the time you are instantiating or after you instantiate object. But not before.
    GameObject go = Instantiate(temp_GameObject) as GameObject;
    go.transform.position = pos_v3;


    or

    Instantiate(temp_GameObject, pos_v3, Quaternion.identity);
     
  3. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,122
    Thanks again zaxvax :) I actually solved my problem after scrutinizing the code and all. The problem was that i forgotten to set the object layer, in the editor, for the prefab as well as the sortingLayerName in the code when i created the prefab. Solved it with this code:

    Code (csharp):
    1.  
    2. temp_GameObject.transform.renderer.sortingLayerName = "CardSortingLayer";
    3.  
    ...now the whole process works with line cast, drag etc.