Search Unity

if statements not executing even condition is true! help please.

Discussion in 'Scripting' started by sush333, Jan 28, 2015.

  1. sush333

    sush333

    Joined:
    Jan 26, 2015
    Posts:
    32
    my if condition is-
    if ((mouseX >= width - 10)) {
    rigidbody.velocity = transform.forward * speed;
    }


    upload_2015-1-29_1-48-29.png

    the condition is true as shown in the screenshot.
    but my body doesn't move. (the move function is correct as i can move it by key press but it doesn't move by this statement)
     
  2. Zaladur

    Zaladur

    Joined:
    Oct 20, 2012
    Posts:
    392
    Show the setting of both mouseX and width. The two variables are in different scripts, so at least one of those variables in your if statement are referencing a variable from another script - you might just be grabbing the value at the start but not updating it.
     
  3. sush333

    sush333

    Joined:
    Jan 26, 2015
    Posts:
    32
    i am constantly updating it by update() method and they are updated in the inspector windows too.

    here is mouseX-
    int mouseX = (int)Input.GetAxis ("Mouse X");

    here is width-
    int width = Screen.width;


    and both comparisons are in int values.
     
  4. sush333

    sush333

    Joined:
    Jan 26, 2015
    Posts:
    32
    Actually i am trying to detect if my cursor is on the edge of the screen.
    if you have any other way then tell me please :)
     
  5. sush333

    sush333

    Joined:
    Jan 26, 2015
    Posts:
    32
    I found that none of the mouse function is working for me onmouseexit onmouseenter onmouseover none of them
     
  6. _met44

    _met44

    Joined:
    Jun 1, 2013
    Posts:
    633
    Hi there, here is what the doc on GetAxis says:

    It just gives you the change the mouse had this very frame, not it's absolute position. Instead you must use Input.mousePosition.

    You should check out visual studio 2013 community edition, it's free for individuals and with it you can use Visual Studio Tools which is provided for free as well and is an awsome debugger. With this set of tools you would have found the issue in the time it takes you to do 3 clicks. (monodevelop also has a debugger but i would not advice using it if you care for you hair).

    Otherwise you can always use Debug.Log(mouseX); to see what's in your variable at the moment it gets used...
     
  7. sush333

    sush333

    Joined:
    Jan 26, 2015
    Posts:
    32
    but the inspector shows mouse x coordinate as 607 and it is not in the range of -1 to 1.
    and also these coordinates are constantly updated in my case.
     
  8. _met44

    _met44

    Joined:
    Jun 1, 2013
    Posts:
    633
    Yeah but this is another script, which probably uses Input.mousePosition...

    Just try to output the mouseX you're using with a debug.log right before your if test.
     
    Last edited: Jan 29, 2015
    sush333 likes this.
  9. Kamalen

    Kamalen

    Joined:
    Dec 27, 2012
    Posts:
    29
    sush333 likes this.
  10. sush333

    sush333

    Joined:
    Jan 26, 2015
    Posts:
    32
    Hey thanks man! that rectangle way is working and detecting my mouse effectively!
    But still none of the onMouse function is working in my unity.
    Anyway you solved my problem.
    Thanks.
     
  11. Kamalen

    Kamalen

    Joined:
    Dec 27, 2012
    Posts:
    29
    The OnMouse(X) functions are sensitive to upper chars, and need a collider too ; make sure your objects respect this and it should work.
     
  12. sush333

    sush333

    Joined:
    Jan 26, 2015
    Posts:
    32
    Actually, the thing is that, the arithmetic operation with screen width and the mouse x position is giving some strange result. that's why i am getting this if statement as false.
    I am gonna investigate about this more.