My OnClick won't trigger
I have a door in my game. I have debuged it and neither the OnClick(), nor the OnMouseOver() will trigger. Here are the Object Details:
Here is my Script: using UnityEngine; using System.Collections; using System.Collections.Generic; using UnityStandardAssets.Characters.FirstPerson; using UnityEngine.UI;
public class EndOfMapDoor : MonoBehaviour
{
public FirstPersonController spieler;
public int TriggerID = 0;
FuseBoxWater myTrigger;
EndOfMap eom = new EndOfMap();
public Text statusLabel;
// Use this for initialization
void Start()
{
List<FuseBoxWater> myTriggers = new List<FuseBoxWater>(GameObject.FindObjectsOfType<FuseBoxWater>());
myTrigger = myTriggers.Find(t => t.FuseID == TriggerID);
eom = GetComponent<EndOfMap>();
}
public string target
{
set
{
eom.NextLevel = value;
}
}
void OnMouseDown()
{
activate();
}
void activate()
{
if (calculateDistance(gameObject.transform.position, spieler.transform.position) < 4)
if (Physics.Raycast(gameObject.transform.position, spieler.transform.position))
{
if (myTrigger.isTriggerd)
{
eom.ChangeLevel(this);
}
else
{
statusLabel.color = new Color(255, 0, 0, 255);
statusLabel.text = "The door has an electric lock";
}
}
}
private float calculateDistance(Vector3 p1, Vector3 p2)
{
float xDiff = p2.x - p1.x;
float yDiff = p2.y - p1.y;
return Mathf.Sqrt(xDiff * xDiff + yDiff * yDiff);
}
}
Answer by phil_me_up · Mar 11, 2016 at 12:19 AM
I think you might be misunderstanding how these things work.
Monobehaviours (i.e. your scripts) don't support events like OnClick() or OnMouseDown() natively. These aren't calls like Start or Update which are essentially called automatically, you need to actually write code to process in input (like the mouse button being held down), and figure out what to do with it (like figure out what object you've actually clicked on).
There are loads of examples of how to do this out there. Here's the first link I found on google which should get you started.
Well, On$$anonymous$$ouseDown is in $$anonymous$$onoBehaviour, so putting it in a script like that should work. But there are three scripts on that door? I think the OP should start with a small example first.
Your answer
![](https://koobas.hobune.stream/wayback/20220612071259im_/https://answers.unity.com/themes/thub/images/avi.jpg)