- Home /
When i press a button on the game scene its starts the game, how i can solve it ?
Hello guys, I had a problem and I have been searching for the solution for a week and I didn't get any result, I have a shop button on the game scene when I press the button it should pop up a scrollRect, I realized I should block raycasts I tried to add graphic raycaster and image that's covers the whole screen to block the raycasts, I tried to add canvas group and doesn't work too, my game its look like subway surf, the game starts when I touch the screen, I have one scene that's content a shop button and the game too, what I want to when I press the shop button its open the scroll rect NOT to start the game PLEASE HELP, I would appreciate who will help me !
MobileInput script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MobileInput : MonoBehaviour {
private const float DEADZONE = 100.0f;
public static MobileInput Instance { set; get; }
private bool tap, swipeLeft, swipeRight, swipeUp, swipeDown;
private Vector2 swipeDelta, startTouch;
public bool Tap { get { return tap; } }
public Vector2 SwipeDelta { get { return swipeDelta; } }
public bool SwipeLeft { get { return swipeLeft; } }
public bool SwipeRight { get { return swipeRight; } }
public bool SwipeUp { get { return swipeUp; } }
public bool SwipeDown { get { return swipeDown; } }
private void Awake()
{
Instance = this;
}
private void Update()
{
//Reseting all the booleans
tap = swipeLeft = swipeRight = swipeUp = swipeDown = false;
//Let's check for inputs
#region Stanedalone Inputs
if (Input.GetMouseButtonDown(0))
{
tap = true;
startTouch = Input.mousePosition;
}
else if (Input.GetMouseButtonUp(0))
{
startTouch = swipeDelta = Vector2.zero;
}
#endregion
#region Mobile Inputs
if (Input.touches.Length != 0)
{
if (Input.touches[0].phase == TouchPhase.Began)
{
tap = true;
startTouch = Input.mousePosition;
}
else if (Input.touches[0].phase == TouchPhase.Ended || Input.touches[0].phase == TouchPhase.Canceled)
{
startTouch = swipeDelta = Vector2.zero;
}
}
#endregion
//Caluclate distance
swipeDelta = Vector2.zero;
if (startTouch != Vector2.zero)
{
//Let's check with mobile
if (Input.touches.Length != 0)
{
swipeDelta = Input.touches[0].position - startTouch;
}
//Lets check with standalone
else if (Input.GetMouseButton(0))
{
swipeDelta = (Vector2)Input.mousePosition - startTouch;
}
}
//Let's check if we're beyond the deadzone
if (swipeDelta.magnitude > DEADZONE)
{
// this is a confirmed swipe
float x = swipeDelta.x;
float y = swipeDelta.y;
if (Mathf.Abs(x) > Mathf.Abs(y))
{
// Left or right
if (x < 0)
swipeLeft = true;
else
swipeRight = true;
}
else
{
// Up or Down
if (y < 0)
swipeDown = true;
else
swipeUp = true;
}
startTouch = swipeDelta = Vector2.zero;
}
}
}
CameraMotor script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraMotor : MonoBehaviour {
public Transform MainCharacter; // Our character // Object we're looking at
public Vector3 offest = new Vector3(0, 5.0f, -10.0f);
public Vector3 rotation = new Vector3(15, 0, 0);
public bool IsMoving { set; get; }
void Update()
{
}
private void LateUpdate()
{
if (!IsMoving)
return;
Vector3 desiredPosition = MainCharacter.position + offest;
desiredPosition.x = 0;
transform.position = Vector3.Lerp(transform.position, desiredPosition, Time.deltaTime);
transform.rotation = Quaternion.Lerp(transform.rotation,Quaternion.Euler(rotation),0.1f);
}
}
GameManager script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GameManager : MonoBehaviour {
private const int COIN_SCORE_AMOUNT = 1;
public static GameManager Instance { set; get; }
public bool isDead { set; get; }
private bool isGameStarted = false;
private PlayerMotor motor;
// UI and UI fields
public Animator gameCanvas, MenuAnim, diamondAnim;
public Text scoreText, coinText, modifierText, hiscoreText;
private float score, coinScore, modifierScore;
public int coinsToAdd;
private int lastScore;
//Death Menu
public Animator deathMenuAnim;
public Text DeadScoreText, DeadCoinText;
//Hats shop
public List<int> hatPrices;
public Transform hatContainer;
public Transform hatButtonContainer;
private int unlockedHats = 1;
//Test
public GameObject backButton;
public GameObject arrowsPanel;
public GameObject currencyText;
public GameObject hidingShopButtonFromTheShop;
public Text currencyTxt;
public ScrollRect myScrollRect;
private void Awake()
{
Instance = this;
modifierScore = 1;
motor = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerMotor>();
modifierText.text = "x" + modifierScore.ToString("0.0");
coinText.text = coinScore.ToString("0");
scoreText.text = scoreText.text = score.ToString("0");
hiscoreText.text = PlayerPrefs.GetInt("Hiscore").ToString();
backButton.SetActive(false);
arrowsPanel.SetActive(false);
currencyText.SetActive(false);
hidingShopButtonFromTheShop.SetActive(true);
}
private void Update()
{
if (MobileInput.Instance.Tap && !isGameStarted)
{
isGameStarted = true;
motor.StartRunning();
FindObjectOfType<GlacierSpawner>().IsScrolling = true;
FindObjectOfType<CameraMotor>().IsMoving = true;
gameCanvas.SetTrigger("Show");
MenuAnim.SetTrigger("Hide");
}
if (isGameStarted && !isDead)
{
//Bump up the score
score += (Time.deltaTime * modifierScore);
if (lastScore != (int)score)
{
lastScore = (int)score;
scoreText.text = score.ToString("0");
}
}
}
public void GetCoin()
{
CoinManager.SavingTheCoins(coinsToAdd);
diamondAnim.SetTrigger("Collect");
coinScore++;
coinText.text = coinScore.ToString("0");
score += COIN_SCORE_AMOUNT;
scoreText.text = score.ToString("0");
}
public void UpdateModifier(float modifierAmount)
{
modifierScore = 1.0f + modifierAmount;
modifierText.text = "x" + modifierScore.ToString("0.0");
}
public void OnDeath()
{
isDead = true;
FindObjectOfType<GlacierSpawner>().IsScrolling = false;
DeadScoreText.text = score.ToString("0");
DeadCoinText.text = coinScore.ToString("0");
deathMenuAnim.SetTrigger("Dead");
gameCanvas.SetTrigger("Hide");
// Check if this is a highscore
if (score > PlayerPrefs.GetInt("Hiscore"))
{
float s = score;
if (s % 1 == 0)
s += 1;
PlayerPrefs.SetInt("Hiscore", (int)s);
}
}
//Hats
private void SetHatMenu()
{
// Price
int i = 0;
foreach (Transform t in hatButtonContainer)
{
// If already unlocked
if ((unlockedHats & 1 << i) == 1 << i)
t.GetChild(1).gameObject.SetActive(false);
else
t.GetChild(1).GetComponentInChildren<Text>().text = hatPrices[i].ToString();
i++;
}
}
{
// If unlocked hats
if ((unlockedHats & 1 << index) == 1 << index)
{
Debug.Log(unlockedHats);
//Physical changes
foreach (Transform t in hatContainer)
t.gameObject.SetActive(false);
if (index == 0)
return;
hatContainer.GetChild(index - 1).gameObject.SetActive(true);
}
else
{
if (CoinManager.Coins >= hatPrices[index])
{
CoinManager.Coins -= hatPrices[index];
// Unlock in array
unlockedHats += 1 << index;
// Physical change
foreach (Transform t in hatContainer)
t.gameObject.SetActive(false);
if (index == 0)
return;
hatContainer.GetChild(index - 1).gameObject.SetActive(true);
hatButtonContainer.GetChild(index).GetChild(1).gameObject.SetActive(false);
Debug.Log(hatPrices);
}
}
}
//Dead panel buttons
public void OnRestartButton()
{
UnityEngine.SceneManagement.SceneManager.LoadScene("Game");
}
public void OnShopClick()
{
myScrollRect.horizontalNormalizedPosition = 0.5f;
}
public void OnStartClick()
{
UnityEngine.SceneManagement.SceneManager.LoadScene("Game");
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612144533im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Shift gear from 2 sliders? 1 Answer
Multi - touch not registering during update. 0 Answers
How to Use Key Combinations with the Control-Key? 2 Answers
Collision Not Working 1 Answer
C# Touch Script - Fix GameObject touch. 0 Answers