unity не работает код на android
В редакторе все работает исправно. Должно после нажатия на кнопку кнопка исчезать и выполнять функцию. После компиляции на андроид кнопки исчезают сразу и не выполняют ничего - т.е. если я нажимаю на экран, начинают создаваться кубы, начинается игра, кнопки исчезают. На всякий случай скидываю весь код.
using System.Collections.Generic;
using System;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class GamwController : MonoBehaviour
{
private CubePos nowCube = new CubePos(0, 1, 0);
public float cubeChangePlaceSpeed = 0.5f;
public Transform cubeToPlace;
private float camMoveToYPosition, camMoveSpeed = 2f;
public Text scorTxt;
public GameObject[] cubesTocreate;
public GameObject allCubs, vfx;
public GameObject[] canvasStartPage;
private Rigidbody allCubsRb;
public Color[] bgcolors;
private Color toCameraColor;
private bool IsLose, firstCube;
private List<Vector3> allCubesPositions = new List<Vector3> {
new Vector3(0, 0, 0),
new Vector3(1, 0, 0),
new Vector3(-1, 0, 0),
new Vector3(0, 1, 0),
new Vector3(0, 0, 1),
new Vector3(0, 0, -1),
new Vector3(1, 0, 1),
new Vector3(-1, 0, -1),
new Vector3(-1, 0, 1),
new Vector3(1, 0, -1),
};
private int prevCountMaxHorizontal;
private Transform maiCam, Camfinish;
private Coroutine showCubePlace;
private List<GameObject> posibleCubsToCreate = new List<GameObject>();
private void Start()
{
if (PlayerPrefs.GetInt("score") < 8)
posibleCubsToCreate.Add(cubesTocreate[0]);
else if (PlayerPrefs.GetInt("score") < 15)
AddPosibleCubs(2);
else if (PlayerPrefs.GetInt("score") < 20)
AddPosibleCubs(3);
else if (PlayerPrefs.GetInt("score") < 25)
AddPosibleCubs(4);
else if (PlayerPrefs.GetInt("score") < 30)
AddPosibleCubs(5);
else AddPosibleCubs(10);
scorTxt.text = "<size=35><color=#630C0C>Best:</color></size> " + PlayerPrefs.GetInt("score") + "\n<size=30>Now:</size> 0";
toCameraColor = Camera.main.backgroundColor;
maiCam = Camera.main.transform;
camMoveToYPosition = 5.9f + nowCube.y - 1f;
allCubsRb = allCubs.GetComponent<Rigidbody>();
showCubePlace = StartCoroutine(ShowCubePlace());
}
private void Update()
{
// пофиксить условию > 0 на != 0 //
if ((Input.GetMouseButtonDown(0) || Input.touchCount != 0) && cubeToPlace != null && allCubs != null && !EventSystem.current.IsPointerOverGameObject())
{
#if !UNITY_EDITOR
if (Input.GetTouch(0).phase != TouchPhase.Began)
return;
#endif
if (!firstCube)
{
firstCube = true;
foreach (GameObject obj in canvasStartPage)
Destroy(obj);
}
GameObject creeteCubs = null;
if (posibleCubsToCreate.Count == 1)
creeteCubs = posibleCubsToCreate[0];
else
creeteCubs = posibleCubsToCreate[UnityEngine.Random.Range(0, posibleCubsToCreate.Count)];
GameObject newCube = Instantiate(
creeteCubs,
cubeToPlace.position,
Quaternion.identity) as GameObject;
newCube.transform.SetParent(allCubs.transform);
nowCube.setVector(cubeToPlace.position);
allCubesPositions.Add(nowCube.getVector());
if (PlayerPrefs.GetString("music") != "No")
GetComponent<AudioSource>().Play();
GameObject newvfx = Instantiate(vfx, cubeToPlace.position,Quaternion.identity) as GameObject;
Destroy(newvfx, 1.5f);
allCubsRb.isKinematic = true;
allCubsRb.isKinematic = false;
SpawnPositions();
MowCameraChangeBg();
}
if (!IsLose && allCubsRb.velocity.magnitude > 0.1f)
{
Destroy(cubeToPlace.gameObject);
IsLose = true;
StopCoroutine(showCubePlace);
Camfinish = Camera.main.transform;
Camfinish.localPosition = new Vector3(0, 8, -20);
}
maiCam.localPosition = Vector3.MoveTowards(maiCam.localPosition,
new Vector3(maiCam.localPosition.x, camMoveToYPosition, maiCam.localPosition.z),
camMoveSpeed * Time.deltaTime);
if (Camera.main.backgroundColor != toCameraColor)
Camera.main.backgroundColor = Color.Lerp(Camera.main.backgroundColor, toCameraColor, Time.deltaTime / 1.5f);
}
IEnumerator ShowCubePlace()
{
while (true)
{
SpawnPositions();
yield return new WaitForSeconds(cubeChangePlaceSpeed);
}
}
private void SpawnPositions()
{
List<Vector3> positions = new List<Vector3>();
if(IsPositionEmpty(new Vector3(nowCube.x + 1, nowCube.y, nowCube.z))
&& nowCube.x + 1 != cubeToPlace.position.x)
positions.Add(new Vector3(nowCube.x + 1, nowCube.y, nowCube.z));
if (IsPositionEmpty(new Vector3(nowCube.x - 1, nowCube.y, nowCube.z))
&& nowCube.x - 1 != cubeToPlace.position.x)
positions.Add(new Vector3(nowCube.x - 1, nowCube.y, nowCube.z));
if (IsPositionEmpty(new Vector3(nowCube.x, nowCube.y + 1, nowCube.z))
&& nowCube.y + 1 != cubeToPlace.position.y)
positions.Add(new Vector3(nowCube.x, nowCube.y + 1, nowCube.z));
if (IsPositionEmpty(new Vector3(nowCube.x, nowCube.y, nowCube.z + 1))
&& nowCube.z + 1 != cubeToPlace.position.z)
positions.Add(new Vector3(nowCube.x, nowCube.y, nowCube.z + 1));
if (IsPositionEmpty(new Vector3(nowCube.x, nowCube.y - 1, nowCube.z))
&& nowCube.y - 1 != cubeToPlace.position.y)
positions.Add(new Vector3(nowCube.x, nowCube.y - 1, nowCube.z));
if (IsPositionEmpty(new Vector3(nowCube.x, nowCube.y, nowCube.z - 1))
&& nowCube.z - 1 != cubeToPlace.position.z)
positions.Add(new Vector3(nowCube.x, nowCube.y, nowCube.z - 1));
if (positions.Count > 1)
cubeToPlace.position = positions[UnityEngine.Random.Range(0, positions.Count)];
else if (positions.Count == 0)
IsLose = true;
else
cubeToPlace.position = positions[0];
}
private bool IsPositionEmpty(Vector3 targetPos)
{
if (targetPos.y == 0)
return false;
foreach(Vector3 pos in allCubesPositions)
{
if (pos.x == targetPos.x && pos.y == targetPos.y && pos.z == targetPos.z)
return false;
}
return true;
}
private void MowCameraChangeBg()
{
int maxX = 0, maxY = 0, maxZ = 0, maxHor;
foreach (Vector3 pos in allCubesPositions)
{
if (Mathf.Abs(Convert.ToInt32(pos.x)) > maxX)
maxX = Convert.ToInt32(pos.x);
if (Convert.ToInt32(pos.y) > maxY)
maxY = Convert.ToInt32(pos.y);
if (Mathf.Abs(Convert.ToInt32(pos.z)) > maxZ)
maxZ = Convert.ToInt32(pos.z);
}
maxY--;
if (PlayerPrefs.GetInt("score") < maxY)
PlayerPrefs.SetInt("score", maxY);
scorTxt.text = "<size=35><color=#630C0C>Best:</color></size> " + PlayerPrefs.GetInt("score") + "\n<size=30>Now:</size>" + maxY;
camMoveToYPosition = 5.9f + nowCube.y - 1f;
maxHor = maxX > maxZ ? maxX : maxZ;
if (maxHor % 3 == 0 && prevCountMaxHorizontal != maxHor)
{
maiCam.localPosition -= new Vector3(0, 0, 3f);
prevCountMaxHorizontal = maxHor;
}
if (maxY >= 200)
toCameraColor = bgcolors[2];
else if (maxY >= 100)
toCameraColor = bgcolors[1];
else if (maxY >= 50)
toCameraColor = bgcolors[0];
if (maxY >= 8)
cubeChangePlaceSpeed = 0.5f;
if (maxY >= 15)
cubeChangePlaceSpeed = 0.4f;
if (maxY >= 20)
cubeChangePlaceSpeed = 0.3f;
if (maxY >= 25)
cubeChangePlaceSpeed = 0.2f;
if (maxY >= 30)
cubeChangePlaceSpeed = 0.1f;
}
private void AddPosibleCubs(int till)
{
for (int i = 0; i < till; i++)
posibleCubsToCreate.Add(cubesTocreate[i]);
}
}
struct CubePos
{
public int x, y, z;
public CubePos(int x, int y, int z)
{
this.x = x;
this.y = y;
this.z = z;
}
public Vector3 getVector()
{
return new Vector3(x, y, z);
}
public void setVector(Vector3 pos)
{
x = Convert.ToInt32(pos.x);
y = Convert.ToInt32(pos.y);
z = Convert.ToInt32(pos.z);
}
}```