- Home /
Trying to make a button interactable
Hello,
I've search the forums and found similar topics but haven't found on the helps with this specific issue. I am trying to get a button to be interactable once the player comes into contact with a collider (set to a trigger) Below is the code I have:
[RequireComponent(typeof(Button))]
public class SpellBook : MonoBehaviour
{
public Button disarm;
public Sprite blockA;
public bool interactable;
public GameObject trigA;
private int counter = 0;
// public event TriggerEventDelegate triggerEvent;
// Start is called before the first frame update
void Start()
{
disarm = GetComponent<Button>();
disarm.interactable = false;
//disarm.Sprite.enabled = false;
}
public void OnMouseDown()
{
DestroyObject(GameObject.FindGameObjectWithTag("Trap"));
}
public void OnTriggerEnter()
{
//disarm = GameObject.FindGameObjectWithTag("Prop");
disarm.interactable = !disarm.interactable;
}
}
I have trouble getting this to work myself before, might be a bug. I'm not sure how to fix it, but a work around could be to use a sprite with a collider and a button.
Are you sure the trigger method is called? Do you have the collider/rigidbody that are required?
@fafase - Attached are pictures, I'm pretty sure I have everything right....or not, I've tried 4 times now to upload the images on and it's not working even though it's under the file size limit :/
Answer by kskjadav007 · Jan 22, 2020 at 06:55 AM
@rdmartyr7 I think you need to assign this script to object which has trigger collider.
Thanks! That worked perfectly! Thank you everyone for the assistance. It was much appreciated.
Answer by rdmartyr7 · Jan 21, 2020 at 10:41 PM
@fafase I am fairly certain it is called. I have it set as being called on runtime on the inspector for the button. I have the collider on the object the player should be interacting with to unlock the ability to use that button for one of their abilities and the trigger button is checked on the collider. I will post an image of both inspectors when I get home from work in about an hour and half in case I missed something. ok, it worked in this one but not my direct reply...well here are pictures if anyone can help or let me know what i did wrong that would be awesome.
Answer by yapyuienfeng · Jan 22, 2020 at 04:16 AM
The object that enters the collider must have a rigidbody.
Rigidbody will only work with OnTriggerEnter while Rigidbody2D will only work with OnTriggerEnter2D.
$$anonymous$$y player has an fps controller which has a rigidbody component
Hi, can you add Debug.Log("Collided"); into your OnTriggerEnter function and let us know the result?
Your answer
Follow this Question
Related Questions
How do I stop EventSystem.currentSelectedGameObject from changing on a button press 0 Answers
inheritance vs multiple component for UI 1 Answer
How can i add a event onclick for a ui button that is child of canvas ? 1 Answer
How do I trigger OnSelect() via the ISelectHandler in my class? 1 Answer
Multiple Cars not working 1 Answer