Hello everyone!
As the title suggests, is there a way to programmatically call the OnMouseDown() function of a GameObject?
Hello everyone!
As the title suggests, is there a way to programmatically call the OnMouseDown() function of a GameObject?
Not sure what you mean with that, but you can definitely use OnMouseDown to activate something when “clicked” http://docs.unity3d.com/ScriptReference/MonoBehaviour.OnMouseDown.html
If you mean you have code in your OnClick and want to call it with another method, you could do it like this:
Let’s say you have this right now;
using UnityEngine;
using System.Collections;
public class Example : MonoBehaviour {
void OnMouseDown() // You want to call this right?
{
Debug.Log("Yay!");
}
}
To make it callable, you could:
using UnityEngine;
using System.Collections;
public class Example : MonoBehaviour {
void OnMouseDown() // You want to call this right?
{
RunCode(); // Redirect to the actual code
}
public void AccessibleMethod()
{
RunCode(); // You can call this whenever you want, programmatically
}
private void RunCode()
{
Debug.Log("Yay!");
}
}
Now you can call the “OnClick / OnMouseDown” Code programmatically too ![]()
I hope that helps, if you still have questions, feel free to ask, otherwise please accept this answer ![]()