- Home /
OnMouseOver Problem
public class ButtonTrigger : MonoBehaviour {
public GameObject karakter;
public GameObject ButtonLevel1;
private void OnMouseOver()
{
if (Input.GetMouseButton(0))
{
karakter.SetActive(true);
ButtonLevel1.SetActive(true);
}
}
private void OnMouseOver2()
{
if (Input.GetMouseButton(0))
{
//...
}
}
}
hello I have 3 buttons in the form of 3 dimensions. so I use onmouseover to be able to move to the next scene. but what about the correct code? because I entered the onmouseover code for three buttons in one code he can't, do I have to separate it?
my problem is on the if-statement, I can't make it read, when I click object 1 or 2 or 3, please help me
Answer by SkaredCreations · Nov 14, 2018 at 03:43 PM
You should handle Input methods (like GetMouseButton) inside Update or FixedUpdate in general. You could try to move your logic inside OnMouseDown instead (anyway "OnMouseOver" doesn't exist, I suppose you meant "OnMouseEnter").
Ops, sorry I forgot about it and confused with On$$anonymous$$ouseEnter ;)
Answer by kaosermail · Nov 26, 2018 at 11:55 AM
First I would check with a print if it enters the function:
private void OnMouseOver()
{
print(gameObject.name);
}
If that doesn't work, check colliders and check the script is on each button.
That should work, you don't need to separate the code.
Tell me if that works, pls!
Answer by brunopava · Nov 26, 2018 at 01:04 PM
What I would do is something like this:
using UnityEngine;
using UnityEngine.Events;
//Attach this to all three buttons
public class TriggerButton : MonoBehaviour
{
public CustomEvent onMouseDown;
private void Awake()
{
//Register StandardAction() function to the listener.
//This is an example using code to add the listener
//But you can also drag and drop in the editor just like the UI button actions.
//This way you can use this script for multiple purposes
onMouseDown.AddListener(StandardAction);
}
private void Update()
{
if(Input.GetMouseButtonDown(0))
{
//Invokes all functions that were registered to the listener
onMouseDown.Invoke();
}
}
//Your stuff
private void StandardAction()
{
Debug.Log("DO YOUR OWN STUFF HERE :D");
}
}
//This is to make the event appear in the editor
[System.Serializable]
public class CustomEvent : UnityEvent {}