How to make a gameobject clickable in a certain radius
I'm very new to scripting and I've been following youtube tutorials on how to get some ui elements working in my game. I've made a script to make this cube clickable and changes the active state. I want to make this object clickable in a certain range that is easily adjustable.
public GameObject myobject;
public bool activateme;
void OnMouseDown()
{
if(!EventSystem.current.IsPointerOverGameObject())
// Turns the image on if it is off, and off if it is on.
if (activateme == true)
{
myobject.SetActive(true);
}
else
{
myobject.SetActive(false);
}
}
Here's the script btw
Answer by lgarczyn · Nov 18, 2019 at 01:02 AM
Simply add a larger trigger collider to the object or one of its children. If you add it to its child, move this script to the child in question.
So my gameobjects needs a collider to be clickable so there is already one on it. Do I make a 2nd colider and make it bigger?
Never $$anonymous$$d the solution I found was to make the object that you have to click a child of an empty gameobject with a colider that has is trigger on.
Your answer

Follow this Question
Related Questions
Script doesn't let me disable a second panel and I cant find a way around this. 0 Answers
How Can i Get This bool to work? Just need someone willing to explain. 2 Answers
AddListener fuction throwing a NullReferenceException error 1 Answer
Prefab UI Slider misses the fill and background items... 0 Answers
i try make enemy health slider or any uI just show the health of Enemy Over head 0 Answers