OnMouseEnter OnMouseExit, and Event Triggers Not Working?
I've tried everything, read all docs, and questions/answers i could find, Tried using those methods, Still didnt work at all. I tried 'OnMouseEnter' and 'onMouseEnter' same thing for the OnMouseExit, Still doesnt work.
Edit: My objects have BoxColliders on them, along with rigidbody. I get no errors. Only problem is 'OnMouseEnter();'
Code[Unity 5] (C# Because I don't like using JS):
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class PickupItem : MonoBehaviour {
public Text text;
public string itemname;
public string hoverMessage;
public string pickupMessage;
public bool mouseHover;
public int itemID;
public Inventory inv;
public GameDB invAdd;
public bool[] items;
public int itemAmount;
public bool pickedupItem;
private float texttime;
public float textTime;
void Start () {
texttime = textTime;
inv = GameObject.FindObjectOfType<Inventory> ();
invAdd = GameObject.FindObjectOfType<GameDB> ();
text = GameObject.Find ("ItemPickupText").GetComponent<Text>();
}
void Update () {
if (pickedupItem) {
text.text = "" + pickupMessage + " [" + itemname + " x" + itemAmount + "]";
texttime -= Time.deltaTime;
if (texttime <= 0) {
texttime = textTime;
text.text = "";
pickedupItem = false;
}
}
if (!mouseHover) {
text.text = "";
}
if (mouseHover) {
if (Input.GetMouseButton (0)) {
this.gameObject.transform.position = Input.mousePosition;
}
}
}
//Backup if Event Trigger Doesnt work
public void OnMouseEnter(){
Hover (true);
}
public void OnMouseExit(){
Hover (false);
}
public void Hover(bool h){
mouseHover = h;
if (mouseHover) {
text.text = "" + hoverMessage + " [" + itemname + " x" + itemAmount + "]";
if (Input.GetKeyDown (KeyCode.E)) {
if (items [0] = true) {
invAdd.AddWaterBottle (itemAmount);
pickedupItem = true;
}
if (items [1] = true) {
invAdd.AddChocolateBar (itemAmount);
pickedupItem = true;
}
if (items [2] = true) {
invAdd.AddCannedBeans (itemAmount);
pickedupItem = true;
}
if (items [3] = true) {
invAdd.AddGauze (itemAmount);
pickedupItem = true;
}
if (items [4] = true) {
invAdd.AddAxe (itemAmount);
pickedupItem = true;
}
}
}
}
}
Hmm. Dont you want On$$anonymous$$ouseOver(), anyway? Enter and exit are only called once and in your hover function you are trying to detect for a Down $$anonymous$$ey Press. That'll take some ti$$anonymous$$g.
This script is on an object with a collider, correct?
Yeah, i figured it out :) thanks anyways, It was working the entire time, the only problem was my "Hover" void.. I fixed it and it works fine :D
Answer by Tyche10 · May 11, 2016 at 11:39 AM
Did you set the collider components to IsTrigger?
"MonoBehaviour.OnMouseEnter() is called on Colliders marked as Trigger if and only if Physics.queriesHitTriggers is true." (http://docs.unity3d.com/ScriptReference/MonoBehaviour.OnMouseEnter.html)
Yes, I found out why it wasnt working, It wasnt calling my "Hover" function, Found that out by adding a Debug.Log(); in the On$$anonymous$$ouseEnter() function.. When the mouse is hovering its meant to show text, and it does now :D but for some reason it doesnt work properly when i have multiple objects (Loot) in the scene with that script? here: http://imgur.com/fig7Pms
Sorry, I don't think I can help you with that without testing the results myself.
No problem. If the answer I gave was the correct one, feel free to accept it ;)