Search Unity

OnMouseDown works once and never again

Discussion in 'Scripting' started by HeywoodFloyd, Jan 18, 2010.

  1. HeywoodFloyd

    HeywoodFloyd

    Joined:
    May 27, 2009
    Posts:
    16
    Hi,

    I've been trying what should be ridiculously simple, but I haven't been able to get it to work all day. I've got a door that I want to open when I click on it and not penetrate when I approach it. Because the door is kind of complicated, I put a box collider on the geometry. The box collider contains the door. I attached the script below. The collider isn't a trigger because I don't want my FPS avatar to be able to get through it.

    Everything works the first time I click the door, if I haven't moved my FPS avatar. After that, no clicks are detected. Also, no clicks are detected after I've moved my avatar.

    I had several cameras in the scene, but because somebody w/ a similar problem solved it by turning off cameras, I turned off all but the character controller camera. Still saw the same problem.

    This is something that should've taken five minutes that I've spent all day on. Any help would be appreciated.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System;
    4. using System.Collections;
    5.  
    6. public class DoorOpener : MonoBehaviour
    7. {
    8.     public float rotationAmount = -90.0f;
    9.     public float speed = 90.0f;
    10.     public Vector3 rotationAxis = Vector3.up;
    11.     public float pauseTime = 2.0f;
    12.  
    13.     float m_direction;
    14.     void Start()
    15.     {
    16.         m_direction = Math.Sign(rotationAmount);
    17.     }
    18.  
    19.     void OnMouseDown()
    20.     {
    21.         Debug.Log("||>-||>-||>-||>-||>-||>-||>-||>-||>-||>-||>-||>-||>-||>-||>-||>-||>-||>-||>-||>-||>-");
    22.         StartCoroutine(rotateDoor());
    23.     }
    24.  
    25.     IEnumerator rotateDoor()
    26.     {
    27.         float currentAngle = 0;
    28.         Quaternion originalRotation = transform.parent.localRotation;
    29.         while(Math.Abs(currentAngle) < Math.Abs(rotationAmount))
    30.         {
    31.             float dTheta = Time.deltaTime * speed * m_direction;
    32.             transform.parent.Rotate(rotationAxis * dTheta);
    33.             currentAngle += dTheta;
    34.             yield return currentAngle;
    35.         }
    36.  
    37.         yield return new WaitForSeconds(pauseTime);
    38.  
    39.         while(m_direction * currentAngle  > 0)
    40.         {
    41.             float dTheta = Time.deltaTime * speed * -m_direction;
    42.             transform.parent.Rotate(rotationAxis * dTheta);
    43.             currentAngle += dTheta;
    44.             yield return currentAngle;
    45.         }
    46.         transform.parent.localRotation = originalRotation;
    47.        
    48.     }
    49.  
    50. }
    51.  
    52.  
     
  2. Xspeed

    Xspeed

    Joined:
    Apr 16, 2015
    Posts:
    47
    After six years, the same problem again for me.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ObjControl : MonoBehaviour {
    5.     public Animator obj;
    6.     int swt = 0;
    7.  
    8.     // Use this for initialization
    9.     void Start () {
    10.  
    11.     }
    12.  
    13.     // Update is called once per frame
    14.     void Update () {
    15.         if (Input.GetKeyDown ("d")) {
    16.             Debug.Log(swt);
    17.         }
    18.     }
    19.  
    20.     void OnMouseUpAsButton(){
    21.         if (swt == 0) {
    22.             obj.Play ("state1");
    23.             swt = 1;
    24.         } else {
    25.             obj.Play ("state2");
    26.             swt = 0;
    27.         }
    28.     }
    29. }
    If anyone found any solutions, please reply
     
    Last edited: Sep 21, 2015
  3. Jordi-Bonastre

    Jordi-Bonastre

    Unity Technologies

    Joined:
    Jul 6, 2015
    Posts:
    102
    Hi @Xspeed , I attached your script to a Sphere in an empty scene, and it works fine (Unity 5.2.0p1). Try to change the obj.Play for a Debug.Log and you could discard any problem with the Animator.

    If you continue suffering this issue, could you report a new bug, please?
     
  4. AlanMattano

    AlanMattano

    Joined:
    Aug 22, 2013
    Posts:
    1,501
    Make a fresh new script copy and pass the content only (updating the correct public class name). restart Unity just in case.
     
  5. Xspeed

    Xspeed

    Joined:
    Apr 16, 2015
    Posts:
    47
    thank you for your replies.
    The problem is on collider. The colliders are not moved (Animated) with the Mesh. they are invisible, so i cannot find it. Now i have added all colliders into the object. and made some changes in script. So now i can animate the object easily.

    There Is No Problem with the script or Game engine