Search Unity

Code only working in editor.

Discussion in 'Scripting' started by mVlipka, Jan 19, 2011.

  1. mVlipka

    mVlipka

    Joined:
    Jun 2, 2010
    Posts:
    42
    Hello everyone, I am back from a Unity hiatus, but I am having a very strange problem.
    Apparently when I run my code in Unity, everything works fine and dandy, but when I run it through the executable, it breaks. I have tried this on my computer, and have sent it over to my friend only to achieve the same results.

    What is supposed to happen is:
    1. Click on the ball.
    2. If mouse is held down -> ball moves with mouse
    3. If mouse is up -> ball is released

    This all works in the editor, but when I run it, it goes
    1. Click on ball, nothing happens
    2. Move the ball by colliding with it, click on ball, sometimes works
    3. If mouse is up -> ball is released
    4. If mouse is down anywhere except on the ball, the ball is teleported to you.

    I don't see how this would happen seeing as the code is exactly the same.

    Code (csharp):
    1.     enum Weapon
    2.     {
    3.         None = 0
    4.     };
    5.     Rigidbody carriedObject;
    6.     Weapon weapon = Weapon.None;
    7.     // Use this for initialization
    8.     void Start () {
    9.         //Screen.lockCursor = true;
    10.  
    11.     }
    12.    
    13.     // Update is called once per frame
    14.     void FixedUpdate () {
    15.         if (Input.GetButtonDown("Fire1"))
    16.         {
    17.             if (weapon == Weapon.None)
    18.             {
    19.                 RaycastHit outInfo;
    20.                 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    21.                 if (Physics.Raycast(ray, out outInfo, 35, 1 << 8))
    22.                 {
    23.                     outInfo.transform.position = outInfo.point;
    24.                     carriedObject = outInfo.rigidbody;
    25.                     outInfo.rigidbody.position = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Input.mousePosition.z + 20));
    26.                     outInfo.rigidbody.velocity = new Vector3(0, 0, 0);
    27.                 }
    28.             }
    29.         }
    30.         if (Input.GetButton("Fire1"))
    31.         {
    32.             if (carriedObject != null)
    33.             {
    34.                 carriedObject.position = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Input.mousePosition.z + 20));
    35.                 carriedObject.velocity = Vector3.zero;
    36.             }
    37.         }
    38.         if (Input.GetButtonUp("Fire1"))
    39.         {
    40.             carriedObject = null;
    41.         }
    42.     }
    43. }
    Maybe I am overlooking something, but I'm pretty sure everything is right (considering how it works in the editor).

    Any help is appreciated!

    (I'd upload the rar, but I keep getting upload failed).
     
  2. mVlipka

    mVlipka

    Joined:
    Jun 2, 2010
    Posts:
    42
    Well, I'm an idiot.

    I had FixedUpdate instead of Update, but how come I can't use FixedUpdate for this?