Can i disable a function in another script?
Idk how to explain, i made a button that pauses the game (left top corner) it`s working good but the problem is that when i touch that button, my player object also gets the touch input for his script(it`s jumping) Here is my player script :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class Player : MonoBehaviour
{
public Rigidbody2D rb;
public int health=1;
public GameObject gameOver;
public Animator anim;
void Start()
{
rb = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
}
void Update()
{
if (health <= 0)
{
gameOver.SetActive(true);
Destroy(gameObject);
}
if (health == 1)
{
anim.Play("Player1");
}
else if (health==2)
{
anim.Play("Player_shield");
}
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began || Input.GetMouseButton(0))
{
anim.SetFloat("runMultiplier", 1f);
rb.velocity = new Vector2(rb.velocity.x, 7);
rb.velocity = new Vector2(rb.velocity.x, 7);
}
else
{
anim.SetFloat("runMultiplier", 0.7f);
}
}
}
and here is my pause button script :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class PauseButton : MonoBehaviour
{
public GameObject PauseMenuUI;
public void Pause()
{
PauseMenuUI.SetActive(true);
Time.timeScale = 0f;
}
}
So when i press the pause button, the player also jumps and when i exit the pause menu it is going up(continues the movement input), i thought about disabling the update function on player whenever i press the pause button, but i don`t know how and not sure if it`ll work. Can you give me some tips? Thank you!
Answer by tormentoarmagedoom · Feb 29, 2020 at 01:23 AM
Hello.
You dont need to disable anything.
Just need to be sure at the touch function to be sure you are not touching a UI element. something with this structure:
if (Input touch)
{
if (Touching UI)
{
nothing (or not)
}
else
{
Jump
}
}
Bye!
Thanks, i found this function using UnityEngine.EventSystems;
/// <summary>
/// Checks if the pointer, or any touch is over (Raycastable) UI.
/// WARNING: ONLY WOR$$anonymous$$S RELIABLY IF IN LateUpdate/LateTickable!
/// </summary>
private bool IsPointerOverUIObject()
{
if (EventSystem.current.IsPointerOverGameObject())
return true;
for (int touchIndex = 0; touchIndex < Input.touchCount; touchIndex++)
{
Touch touch = Input.GetTouch(touchIndex);
if (EventSystem.current.IsPointerOverGameObject(touch.fingerId))
return true;
}
return false;
}
it`s checking if it`s an UI touched and it`s working with this, ty.
Your answer
Follow this Question
Related Questions
How to disable mouse lock script when returning to main menu 1 Answer
C# Disabling Camera moving while in Pause Menu 1 Answer
How can i enable and disable a void function on the update function? 1 Answer
How to make a UI Image appear/disappear? 2 Answers
Problem in disabling button and error in the statement 0 Answers