Search Unity

Very strange script problem variable change with no reason

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

  1. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,191
    I have very strange script problem that i totally do not understand. It started with this problem/post: http://forum.unity3d.com/threads/why-does-this-code-not-create-a-reaction.360001/

    I have done some testing and really do not understand what is happening.

    Here is code:

    Definition:
    private float testing;

    voidAwake() {
    testing = -99.9f;

    public void OneFingerDragProcess (Gesture gesture) {
    print ("testing: " + testing);

    There is an if-statement before the print statement. The "OneFingerDragProcess" is EasyTouch. The "testing" statements is the only places that they are in the code.

    When i drag the object and trigger testing i get the following results:

    testing: -99.9
    testing: 0
    testing: -99.9
    testing: 0
    testing: -99.9
    testing: 0
    ...

    What i do not understand is why variable testing is changing value all the time. Again, there is only the above places were "testing" is used in the whole project.

    I have other similar problems with other variables as well.

    I know this is a strange problem and would just want to ask if anyone seen anything similar?

    The code were the drag is happening is displayed below. Except this the only place other were the "testing" is used is the definition and in Awake.

    I am puzzled.

    Code (CSharp):
    1. //=======================================================================================//
    2.     //                                 OneFingerDragProcess                                  //
    3.     //=======================================================================================//
    4.  
    5.     public void OneFingerDragProcess (Gesture gesture) {
    6.  
    7.         if (!isCardProtected) { // DO NOT PROCESS IF CARD IS PROTECTED, SET IN "SingleTap"
    8.  
    9.             if (gesture.touchCount == 1) { // CHECK NUMBER OF FINGERS ON THE DISPLAY
    10.              
    11.                 if (selected_GameObject != null) { // Make sure that the code is processed ONLY is object is selected
    12.                  
    13.                     if (!fingerSelectionMode) { // Finger draw selection
    14.                      
    15.                         if (selected_GameObject.tag != "Background") {
    16.                          
    17.                             // catch possible exception errors
    18.                             try {
    19.                                 selected_GameObject.transform.position = Camera.main.ScreenToWorldPoint (gesture.position);
    20.                             } catch {
    21.                              
    22.                                 return;
    23.                             }
    24.                          
    25.                             Vector3 temp = selected_GameObject.transform.position;
    26.                             temp.z = nrOfCardsInTheGame - 1; //5;
    27.                             selected_GameObject.transform.position = temp;
    28.                          
    29.                             //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>//
    30.                             // Manage the draw-point so the object is dragged from the point of click
    31.                             Vector3 worldPoint = Camera.main.ScreenToWorldPoint (Input.mousePosition);     //gesture.position);
    32.                             Vector3 posE = selected_GameObject.transform.position;
    33.                             posE.x = worldPoint.x - delta.x;
    34.                             posE.y = worldPoint.y - delta.y;
    35.                             selected_GameObject.transform.position = posE;
    36.                             //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>//
    37.  
    38.                             // SET UP Y-BASED LIMITS FOR CARDS
    39.                             //                    float redPlayerY = redLocalMultiplayerLinePosition.y + cardHeight;         //     -2.7f
    40.                             //                    float bluePlayerY = blueLocalMultiplayerLinePosition.y - cardHeight;     //     2.7f
    41.                          
    42.                          
    43.                             // Check & correct if object is outside of screen boundaries
    44.                             float finalWidth = maxWidth - cardWidth;
    45.                             float finalHeight = maxHeight - cardHeight;
    46.  
    47.                             // ADJUST THE EDGE BASED ON TYPE OF GAME
    48.                             if (selected_GameObject.transform.position.x <= -finalWidth) {
    49.                                 selected_GameObject.transform.position = new Vector3 (-finalWidth, selected_GameObject.transform.position.y, selected_GameObject.transform.position.z);
    50.                             } else if (selected_GameObject.transform.position.x >= finalWidth) {
    51.                                 selected_GameObject.transform.position = new Vector3 (finalWidth, selected_GameObject.transform.position.y, selected_GameObject.transform.position.z);
    52.                             }
    53.                          
    54.                             //                    if (gameBoardType == "DoNormal") {
    55.                             if (selected_GameObject.transform.position.y <= -finalHeight) {
    56.                                 selected_GameObject.transform.position = new Vector3 (selected_GameObject.transform.position.x, -finalHeight, selected_GameObject.transform.position.z);
    57.                             } else if (selected_GameObject.transform.position.y >= finalHeight) {
    58.                                 selected_GameObject.transform.position = new Vector3 (selected_GameObject.transform.position.x, finalHeight, selected_GameObject.transform.position.z);
    59.                             }
    60.  
    61.                             if (gameMode == "LOCAL MULTIPLAYER") {
    62.                                 //print ("useProtectionZoneAtMultiplayer:       >> " + useProtectionZoneAtMultiplayer);
    63.  
    64.                                 if (useProtectionZoneAtMultiplayer) { // THIS IS TO DISABLE THE PROTECTED QUADRANTS IN MULTIPLAYER MODE
    65.  
    66.                                     whatPlayerIsActive = PlayerPrefs.GetString ("ACTIVE PLAYER");
    67.                                     print ("testing: " + testing);
    68.                                     if (whatPlayerIsActive == "BLUE") {
    69.                                         print ("GO-y: " + selected_GameObject.transform.position.y + " redPlayerY: " + redPlayerY);
    70.                                         if (selected_GameObject.transform.position.y >= 1.9f) { //redPlayerY) {
    71.                                             selected_GameObject.transform.position = new Vector3 (selected_GameObject.transform.position.x, 1.9f, selected_GameObject.transform.position.z);
    72.                                             //selected_GameObject.transform.position = new Vector3 (1f, 1f, 0f);
    73.                                         }
    74.                                     } else if (whatPlayerIsActive == "RED") {
    75.  
    76.                                         if (selected_GameObject.transform.position.y <= -1.9f) { //bluePlayerY) {
    77.                                             //selected_GameObject.transform.position = new Vector3 (-1f, -1f, 1f);
    78.                                             selected_GameObject.transform.position = new Vector3 (selected_GameObject.transform.position.x, bluePlayerY, selected_GameObject.transform.position.z);
    79.                                         }
    80.                                     }
    81.                                 }
    82.                             }
    83.                         }
    84.                     } else {
    85.  
    86.                         selectObjectWithFingerDraw ();
    87.                     }
    88.                 }
    89.             }
    90.         }
    91.     }
     
  2. HiddenMonk

    HiddenMonk

    Joined:
    Dec 19, 2014
    Posts:
    987
    If youd like, post an example project for us to go in and try to see what the issue is.
    I dont think there is enough information for us to help you otherwise.
     
  3. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,191
    I finally, after hours of searching, the problem.

    I changed:
    Code (CSharp):
    1. public class LocalCoinWallet : GameEngine_Script {
    to:

    Code (CSharp):
    1. public class LocalCoinWallet : MonoBehaviour {
    ...and it worked again.

    I still do not understand why this is a problem as i do not reference the GameEngine_Script variables from LocalCoinWallet but it still effect a few variables.

    Anyhow, now it works.