The question is answered, right answer was accepted
Why is my button not working?
I have a button with an Event Trigger with a script attached, but when I touch the button (on a mobile device) it doesn't work. Is there something wrong with my script, or do I need to add something to the button? This is my script:`using System.Collections; using System.Collections.Generic; using UnityEngine;
public class TapToStart : MonoBehaviour {
public GameObject homeCanvas;
public GameObject playCanvas;
void Start () {
homeCanvas = GameObject.Find("HomeScreen").GetComponent<GameObject> ();
playCanvas = GameObject.Find("PlayCanvas").GetComponent<GameObject> ();
homeCanvas.SetActive (false);
playCanvas.SetActive (true);
GameObject.Find("Player").GetComponent<PlayerController>().enabled = true;
GameObject.Find("Player").GetComponent<DestroyContact>().enabled = true;
GameObject.Find("GameSpawner").GetComponent<Instantiater>().enabled = true;
}
}
`
well, what does this script has to do with your button? you need an EventSystem, the button on a canvas and a graphicraycaster on your scene (forgot where the raycaster goes).
I have all of those items, but for some reason, I still can't click the button. I looked at this post https://answers.unity.com/questions/889908/i-created-an-ui-button-but-click-does-not-work.html to try to solve my problem, but nothing worked, which is why I thought it was my script that was the issue.
what you are trying to do? the code u have attached is having only start method which execute when script execute.
I'm trying to create a tap to start button for my game, so when the button is clicked, the home screen canvas is disabled and the game screen canvas is enabled.
$$anonymous$$ake sure you have EventSystem enable in your hierarchy .
Answer by gon777 · Feb 08, 2018 at 07:47 AM
If you cannot interact with the button, for example click but button not pushed down, make sure there is no invisible panel (with Raycast Target 'on') or something else on top of that button.
If you can click the button but nothing happens, make sure you register OnClick() event properly.
Event trigger works with input event, like OnPointerEnter() etc. I am not sure what event you used, and not sure what you're trying to achieve, but if you want click event just use OnClick() event in Button component.
Yep, like the other answer says, check event system is in the scene as well.
Edit: Formating, Grammar
I have the RayCast Target box checked in the hierarchy and there is nothing blocking the way of the button. I think I have the OnClick registered properly, and I removed the event trigger part from the hierarchy.
Basically, I want to create a tap to start button but I can't use timescale = 0 and then 1 because I have a particle effect playing and if I do that, it pauses it.
I think you are not registering event properly. $$anonymous$$y suggestion is, create a method call 'OnTapToStart()', then register OnTapToStart() in OnClick()
In OnTapToStart(), disable your canvas there:
void OnTapToStart()
{
homeCanvas = GameObject.Find("HomeScreen").GetComponent();
playCanvas = GameObject.Find("PlayCanvas").GetComponent<GameObject>();
homeCanvas.SetActive(false);
playCanvas.SetActive(true);
GameObject.Find("Player").GetComponent<PlayerController>().enabled = true;
GameObject.Find("Player").GetComponent<DestroyContact>().enabled = true;
GameObject.Find("GameSpawner").GetComponent<Instantiater>().enabled = true;
}
Also try to avoid find objects, it will slow down your game when things get messy. Try to drag and drop your canvas object to your script.
If you want to play a particle effect, create a prefab and attach particle effect to it, instantiate the prefab when tap the button.
Try not to use Start() as a function name unless you know what you are doing, Start() is invoked automatically when script is enabled.
What do you mean by register OnTapTOStart() in Onclick()? Do you mean to change this part
I changed the script to this`using System.Collections; using System.Collections.Generic; using UnityEngine;
public class TapToStart : $$anonymous$$onoBehaviour {
public GameObject homeCanvas;
public GameObject playCanvas;
void OnTapToStart(){
homeCanvas.SetActive (false);
playCanvas.SetActive (true);
GameObject.Find("Player").GetComponent<PlayerController>().enabled = true;
GameObject.Find("Player").GetComponent<DestroyContact>().enabled = true;
GameObject.Find("GameSpawner").GetComponent<Instantiater>().enabled = true;
}
}
but it's still not working. I have the script dragged onto my canvas with the gameobjects attached
Answer by SQ_11 · Feb 08, 2018 at 07:49 AM
Check Event System is there in Hierarchy. If it is there, then in Inspector Window, check Force Module Active true. Check again it works or not.
I have the Event System in the Hierarchy with the Force $$anonymous$$odule Active box checked, but it's not working. Do I need to put something in the First Selected part of the hierarchy?