Search Unity

[SOLVED] Why does this code not create a reaction, variable change for no reason

Discussion in 'Scripting' started by pKallv, Oct 8, 2015.

  1. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,191
    I have used this piece of code to avoid a drag of an object over a specific line, now it does not work anymore and i do not understand what the problem is. The idea is that when "selected_GameObject" passes over "bluePlayerY" the position should reset to that very same position so the object never passes the line.

    Code (CSharp):
    1.  
    2. public void OneFingerDragProcess (Gesturegesture) { ...
    3. ...
    4.  
    5.  
    6.     } else if (whatPlayerIsActive == "RED") {
    7.         print ("E");
    8.         if (selected_GameObject.transform.position.y <= bluePlayerY) {
    9.             print ("F: " + selected_GameObject.name);
    10.             selected_GameObject.transform.position = new Vector3 (0f, 0f, 0f);
    11. //          selected_GameObject.transform.position = new Vector3 (selected_GameObject.transform.position.x, bluePlayerY, selected_GameObject.transform.position.z);
    12.         }
    13.     }
    The output (print) i get indicate that i do grab the object but the position is never triggered:

    Code (CSharp):
    1. E
    2. F: Enemy-01
    It used to work so i am very puzzled and would very much appreciate some help.
     
  2. BenZed

    BenZed

    Joined:
    May 29, 2014
    Posts:
    524
    The problem is somewhere other than this code.

    I'm assuming selected_GameObject must also be having it's position set elsewhere.
     
  3. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,191
    hmmm I guess you are right. I have been looking for that since yesterday. Thanks
     
  4. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,191
    Just can't find the problem :-(

    I have now tested with the following code, with no success even if the indication seems to be that it will position at 0,0,0:

    Code (CSharp):
    1. print ("selected_GameObject.transform.position.y: " + selected_GameObject.transform.position.y + " | bluePlayerY: " + bluePlayerY);
    2. if (selected_GameObject.transform.position.y <= bluePlayerY) {
    3.     print ("BEFORE BLUE: " + selected_GameObject.transform.position);
    4.     selected_GameObject.transform.position = new Vector3 (0f, 0f, 0f);
    5.     print ("AFTER BLUE: " + selected_GameObject.transform.position);
    6. }
    And get the following result:

    Code (CSharp):
    1. selected_GameObject.transform.position.y: -2.026958 | bluePlayerY: -1.9
    2.  
    3. BEFORE BLUE: (0.9, -2.0, 51.0)
    4.  
    5. AFTER BLUE: (0.0, 0.0, 0.0)
    The GameObject still do not reposition to 0, 0, 0 ?
     
  5. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,191
    Strange, this is a bigger portion of the code, what is strange is that the ADJUST THE EDGE BASED ON TYPE OF GAME portion of the code, to avoid object to be dragged outside of the scene, is still working perfectly. When i change the position in that sector to position = 0, 0, 0 it also works as expected?

    This means that a bit down the code the same type of code does not work even if it is the same type of statements?

    Code (CSharp):
    1. // ADJUST THE EDGE BASED ON TYPE OF GAME
    2. if (selected_GameObject.transform.position.x <= -finalWidth) {
    3.     selected_GameObject.transform.position = new Vector3 (-finalWidth, selected_GameObject.transform.position.y, selected_GameObject.transform.position.z);
    4. } else if (selected_GameObject.transform.position.x >= finalWidth) {
    5.     selected_GameObject.transform.position = new Vector3 (finalWidth, selected_GameObject.transform.position.y, selected_GameObject.transform.position.z);
    6. }
    7.                        
    8. if (selected_GameObject.transform.position.y <= -finalHeight) {
    9.     selected_GameObject.transform.position = new Vector3 (selected_GameObject.transform.position.x, -finalHeight, selected_GameObject.transform.position.z);
    10. } else if (selected_GameObject.transform.position.y >= finalHeight) {
    11.     selected_GameObject.transform.position = new Vector3 (selected_GameObject.transform.position.x, finalHeight, selected_GameObject.transform.position.z);
    12. }
    13.  
    14. if (gameMode == "LOCAL MULTIPLAYER") {
    15.     if (useProtectionZoneAtMultiplayer) { // THIS IS TO DISABLE THE PROTECTED QUADRANTS IN MULTIPLAYER MODE
    16.  
    17.     whatPlayerIsActive = PlayerPrefs.GetString ("ACTIVE PLAYER");
    18.                                
    19.     if (whatPlayerIsActive == "BLUE") {
    20.  
    21.         if (selected_GameObject.transform.position.y >= redPlayerY) {
    22.             print ("RED: " + selected_GameObject.name);
    23.             selected_GameObject.transform.position = new Vector3 (0f, 0f, 0f);
    24.         }
    25.     } else if (whatPlayerIsActive == "RED") {
    26.         print ("selected_GameObject.transform.position.y: " + selected_GameObject.transform.position.y + " | bluePlayerY: " + bluePlayerY);
    27.         if (selected_GameObject.transform.position.y <= bluePlayerY) {
    28.             print ("BEFORE BLUE: " + selected_GameObject.transform.position);
    29.             selected_GameObject.transform.position = new Vector3 (0f, 0f, 0f);
    30.             print ("AFTER BLUE: " + selected_GameObject.transform.position);
    31.         }
    32.     }
    33. }
    34. }
     
  6. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,191
    I just moved the code below and put it before the ADJUST THE EDGE BASED ON TYPE OF GAME and it works as expected meaning that the object is moved to 1,1,0. Now i am really puzzled as the same line of codes works above the piece that does not work ??

    Code (CSharp):
    1. if (selected_GameObject.transform.position.y >= redPlayerY) {
    2.     print ("XXXX: " + selected_GameObject.name);
    3.     selected_GameObject.transform.position = new Vector3 (1f, 1f, 0f);
    4. }
    5.  
    6.  
    7.  
    8.  
    9.                            
    10. // ADJUST THE EDGE BASED ON TYPE OF GAME
     
  7. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,191
    Ok i think i have identified the source of problem but are even more puzzled than before.

    The code string that I think causes that problem is:
    Code (CSharp):
    1. if (useProtectionZoneAtMultiplayer) {
    useProtectionZoneAtMultiplayer flips between false and true when i execute the code. The only place i actually update this property is in Awake with:

    Code (CSharp):
    1. // CHECK PROPERTIES FOR MULTIPLAYER GAME
    2. toggleValues = PlayerPrefs.GetString ("setup_tgl_usePlayerProtectionZonesAtMultiplay");
    3.  
    4. if (toggleValues == "TRUE") {
    5.     useProtectionZoneAtMultiplayer = true;
    6. } else {
    7.     useProtectionZoneAtMultiplayer = false;
    8. }
    9. print ("INIT useProtectionZoneAtMultiplayer: " + useProtectionZoneAtMultiplayer);
    The output i get is:
    Code (CSharp):
    1. INIT useProtectionZoneAtMultiplayer: True
    ...and it prints it only once. Despite that the useProtectionZoneAtMultiplayer flips between true and false during the process? ...really do not understand that.

    I even renamed useProtectionZoneAtMultiplayer to be sure there is no impact from outside but with the same result. I have searched all my code but there is only one place were it is updated, above.

    Really puzzled now !
     
  8. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,191