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?
yes all objects already have collider
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
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!
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 {}
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”).