GlandFalangus
@GlandFalangus

Где ошибка в скрипте?

6040d4128e91b002458616.png возникает эта ошибка при перезапуске игры.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;
using UnityEngine.EventSystems;

public class GameController : MonoBehaviour
{
    private CubePos nowCube = new CubePos(0, 1, 0);
    public float cubeChangePlaceSpeed = 0.5f;
    public Transform cubeToPlace;
    private float camMoveToYPosition, camMoveSpeed = 2f;

    public Text scoreTxt;

    public GameObject[] cubesToCreate;

    public GameObject allCubes, vfx;
    public GameObject[] canvasStartPage;
    private Rigidbody allCubesRb;

    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 mainCam;
    private Coroutine showCubePlace;

    private List<GameObject> posibleCubesToCreate = new List<GameObject>();
    private void Start()
    {
        if (PlayerPrefs.GetInt("score") < 5)
            posibleCubesToCreate.Add(cubesToCreate[0]);
        else if (PlayerPrefs.GetInt("score") < 10)
            AddPossibleCubes(2);
        else if (PlayerPrefs.GetInt("score") < 15)
            AddPossibleCubes(3);
        else if (PlayerPrefs.GetInt("score") < 20)
            AddPossibleCubes(4);
        else if (PlayerPrefs.GetInt("score") < 30)
            AddPossibleCubes(5);
        else if (PlayerPrefs.GetInt("score") < 50)
            AddPossibleCubes(6);
        else if (PlayerPrefs.GetInt("score") < 65)
            AddPossibleCubes(7);
        else if (PlayerPrefs.GetInt("score") < 80)
            AddPossibleCubes(8);
        else if (PlayerPrefs.GetInt("score") < 100)
            AddPossibleCubes(9);
        else
            AddPossibleCubes(10);

        scoreTxt.text = "<size=40><color=#D79242>Score:</color></size> " + PlayerPrefs.GetInt("score") + "\n<size=33> now:</size> 0";
        toCameraColor = Camera.main.backgroundColor;
        mainCam = Camera.main.transform;
        camMoveToYPosition = 5.9f + nowCube.y - 1f;

        allCubesRb = allCubes.GetComponent<Rigidbody>();
        showCubePlace =  StartCoroutine(ShowCubePlace());

    }

    private void Update()
    {
        if((Input.GetMouseButtonDown(0) || Input.touchCount > 0) && cubeToPlace != null && allCubes != 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 createCube = null;
            if (posibleCubesToCreate.Count == 1)
                createCube = posibleCubesToCreate[0];
            else
                createCube = posibleCubesToCreate[UnityEngine.Random.Range(0, posibleCubesToCreate.Count)];


            GameObject newCube = Instantiate( createCube, cubeToPlace.position, Quaternion.identity) as GameObject;

            newCube.transform.SetParent(allCubes.transform);
            nowCube.setVector(cubeToPlace.position);
            allCubesPositions.Add(nowCube.GetVector());

            if (PlayerPrefs.GetString("music") != "No")
                GetComponent<AudioSource>().Play();

            Instantiate(vfx, newCube.transform.position, Quaternion.identity);



            allCubesRb.isKinematic = true;
            allCubesRb.isKinematic = false;

            SpawnPositions();
            MoveCameraChangeBg();
        }

        if (!IsLose && allCubesRb.velocity.magnitude > 0.1f)
        {
            Destroy(cubeToPlace.gameObject);
            IsLose = true;
            StopCoroutine(showCubePlace);
        }
        mainCam.localPosition = Vector3.MoveTowards(mainCam.localPosition,
        new Vector3(mainCam.localPosition.x, camMoveToYPosition, mainCam.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 - 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, 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 MoveCameraChangeBg()
    {
        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);

        scoreTxt.text = "<size=40><color=#D79242>Score:</color></size> " + PlayerPrefs.GetInt("score") + "\n<size=33> now:</size> " + maxY;

        camMoveToYPosition = 5.9f + nowCube.y - 1f;

        maxHor = maxX > maxZ ? maxX : maxZ;
        if(maxHor % 3 == 0 && prevCountMaxHorizontal != maxHor)
        {
            mainCam.localPosition -= new Vector3(0, 0, 2.5f);
            prevCountMaxHorizontal = maxHor;
        }

        if (maxY >= 100)
            toCameraColor = bgColors[10];
        else if (maxY >= 90)
            toCameraColor = bgColors[9];
        else if (maxY >= 80)
            toCameraColor = bgColors[8];
        else if (maxY >= 70)
            toCameraColor = bgColors[7];
        else if (maxY >= 60)
            toCameraColor = bgColors[6];
        else if (maxY >= 50)
            toCameraColor = bgColors[5];
        else if (maxY >= 40)
            toCameraColor = bgColors[4];
        else if (maxY >= 30)
            toCameraColor = bgColors[3];
        else if (maxY >= 20)
            toCameraColor = bgColors[2];
        else if (maxY >= 10)
            toCameraColor = bgColors[1];
        else if (maxY >= 5)
            toCameraColor = bgColors[0];
    }

    private void AddPossibleCubes(int till)
    {
        for (int i = 0; i < till; i++)
            posibleCubesToCreate.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);
        }
    }

6040d5e0178b5618164091.png
Прошу ответить подробно.
Заранее спасибо.
  • Вопрос задан
  • 132 просмотра
Решения вопроса 2
@vabka Куратор тега C#
Токсичный
Ну где-то в апдейте произошёл NRE. Посмотрите что располагается на 124 строке и подебажте.
Видимо
allCubesRb = allCubes.GetComponent<Rigidbody>();
возвращает null

<toxic>
Выкинь юнити и иди C# учить. Вернёшься к юнити через пару месяцев и не будешь такие тупые вопросы задавать
</toxic>
Ответ написан
DanceM
@DanceM Куратор тега Unity
Кто хочет -ищет способ, кто не хочет -ищет причину
Ошибка в строке 124, вероятно в переменной allCubesRb нету ссылки на объект, пустая переменная.
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы