- Home /
Event system and three buttons work, but not the fourth.
I have a UI button on a canvas, one of four, and an Event System. All of the other three buttons work. The fourth one sets to interactable and not interactable, though not as intended. The button strangely sets to not interactable when the player lands and interactable while the player is in the air. Additionally, the button won't respond to clicks at all. It is not covered by other sprites or buttons. What is stranger yet is that I had it working earlier.
Here is the code on the button.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class TeleportScript : MonoBehaviour
{
public GameObject Portal1;
public GameObject Portal2;
public GameObject Player;
public AudioSource Audio;
public Button EnterButton;
public void TeleportTo()
{
Audio.Play();
if (PlayerPrefs.GetInt("PortalTo", 0) == 1)
{
Player.transform.position = Portal1.transform.position;
}
else if (PlayerPrefs.GetInt("PortalTo", 0) == 2)
{
Player.transform.position = Portal2.transform.position;
}
EnterButton.interactable = !EnterButton.interactable;
}
}
Here is another code that alters the first script.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PortalScript : MonoBehaviour
{
// Start is called before the first frame update
void OnTriggerEnter2D(Collider2D other)
{
PlayerPrefs.SetInt("PortalTo", 1);
}
}
This last part is a broken-down version of the player's interaction with the portal and button.
bool portal;
//FixedUpdate
if (portal == true)
{
EnterButton.interactable = true;
}
else if (portal == false)
{
EnterButton.interactable = false;
}
void OnTriggerEnter2D(Collider2D other)
else if (other.gameObject.CompareTag("Portal"))
{
portal = true;
}
}
void OnTriggerExit2D(Collider2D other)
{
if (other.gameObject.CompareTag("Portal"))
{
portal = false;
}
Technically, this shouldn't play a factor in this. Here is the player jump function which another button executes.
public void Jump()
{
if (grounded == true)
{
grounded = false;
rigidbody.AddForce(transform.up * jump * 2f);
Anim.Play("Lantern_Jump");
}
}
Your answer
Follow this Question
Related Questions
How to detect button presses and change child object text in the button's Parent Object script 0 Answers
Changing button positions on different resolutions 1 Answer
How to make UI buttons so my player can move along certain lines 0 Answers
Help check click out of UI 2 Answers
Enable UI Button from Prefab 1 Answer