Why Pause and Sound On-Off Working on pc NOT working on Android
Hi everyone. I have a problem with pause and sound buttons. Those are working on PC. And I build to android and not working both of them. When I touch pause button on android play touch sound but not pause the game. When I touch the sound button not working too. Is there anyone else have an idea. Thank you.
This is my Pause Script.
[code=CSharp] using UnityEngine; using System.Collections;
public class Pause : MonoBehaviour {
bool pause = false;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown(0))
{
checkTouch(Input.mousePosition);
}
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
{
checkTouch(Input.GetTouch(0).position);
}
}
void checkTouch(Vector3 pos)
{
Vector3 wp = Camera.main.ScreenToWorldPoint(pos);
Vector2 touchPos = new Vector2(wp.x, wp.y);
var hit = Physics2D.OverlapPoint(touchPos);
if (hit)
{
string name = hit.transform.gameObject.name;
if (name == "pause")
{
SoundsManager.Instance.PlaySound("touch");
pause = !pause;
Time.timeScale = pause ? 0.0f : 1.0f;
}
}
}
} [/code]
And this is my Sound Script.
[code=CSharp]using UnityEngine;
public class SoundOnOff : MonoBehaviour{
private static SoundOnOff _Instance;
public Sprite on;
public Sprite off;
public static SoundOnOff Instance
{
get
{
return SoundOnOff._Instance;
}
}
private void Awake()
{
if (SoundOnOff._Instance == null)
{
SoundOnOff._Instance = this;
GetComponent<SpriteRenderer>().sprite = on;
}
else
{
GetComponent<SpriteRenderer>().sprite = Instance.GetComponent<SpriteRenderer>().sprite;
}
DontDestroyOnLoad(gameObject);
}
private void Start()
{
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
checkTouch(Input.mousePosition);
}
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
{
checkTouch(Input.GetTouch(0).position);
a = Input.GetTouch(0).position.ToString();
}
}
string a;
string obj;
void checkTouch(Vector3 pos)
{
Vector3 wp = Camera.main.ScreenToWorldPoint(pos);
Vector2 touchPos = new Vector2(wp.x, wp.y);
var hit = Physics2D.OverlapPoint(touchPos);
if (hit)
{
string name = hit.transform.gameObject.name;
obj = Instance.GetComponent<SpriteRenderer>().sprite.name;
if (name == "sound")
{
if (Instance.GetComponent<SpriteRenderer>().sprite.name == "sound_on")
{
SoundsManager.Instance.MuteSound();
//GetComponent<SpriteRenderer>().sprite = off;
Instance.GetComponent<SpriteRenderer>().sprite = off;
}
else if(Instance.GetComponent<SpriteRenderer>().sprite.name == "sound_off")
{
SoundsManager.Instance.OpenSound();
Instance.GetComponent<SpriteRenderer>().sprite = on;
//GetComponent<SpriteRenderer>().sprite = on;
}
}
}
}
void OnGUI()
{
int w = Screen.width, h = Screen.height;
GUIStyle style = new GUIStyle();
Rect rect = new Rect(0, 100, w, h * 4 / 200);
style.alignment = TextAnchor.UpperLeft;
style.fontSize = h * 2 / 50;
style.normal.textColor = new Color(1f, 1f, 1f, 1.0f);
string text = string.Format(a + "-" + obj);
GUI.Label(rect, text, style);
}
}[/code]
And this is SoundActivater
[code=CSharp]using UnityEngine; using System.Collections;
public class SoundActiver : MonoBehaviour {
public GameObject sound;
void Awake()
{
DontDestroyOnLoad(gameObject);
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
OnLevelWasLoaded(Application.loadedLevel);
}
void OnLevelWasLoaded(int level)
{
if (level == 9)
sound.SetActive(false);
else
sound.SetActive(true);
}
} [/code]
Comment