Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Reals7eel · May 11, 2016 at 11:22 AM · c#event triggeringonmouseenteronmouseexit

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;
             }
         }
     }
 }
 }
Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image meat5000 ♦ · May 11, 2016 at 12:30 PM 0
Share

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?

avatar image Reals7eel meat5000 ♦ · May 11, 2016 at 12:33 PM 0
Share

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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)

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Reals7eel · May 11, 2016 at 11:51 AM 0
Share

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

avatar image Tyche10 Reals7eel · May 11, 2016 at 12:00 PM 0
Share

Sorry, I don't think I can help you with that without testing the results myself.

avatar image Reals7eel · May 11, 2016 at 12:02 PM 0
Share

Its alright, thanks anyways :D

avatar image Tyche10 Reals7eel · May 11, 2016 at 12:17 PM 0
Share

No problem. If the answer I gave was the correct one, feel free to accept it ;)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

157 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Color transtition not rendering 1 Answer

Add Event Trigger parameters using C# 1 Answer

Detect direction of drag (Event Trigger) 0 Answers

EventTrigger multitouch 0 Answers

Method not being called on Unity Event 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges