How to make scoring system
Hello I am new to the game development and I've been tasked by my profesor to make tetris game. The game itself is finnished but I have huge problem with implementing scoring system as I just dont know how to extract points from one class (where everything is happening) to the other class (which is responsible of showing the score on the screen). Here is the main code where everything is happening and where points are counted
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class TetrisBlock : MonoBehaviour { public Vector3 rotationPoint; private float previousTime; public float fallTime = 0.8f; public static int height = 20; public static int width = 10; private static Transform[,] grid = new Transform[width, height]; public int score; public int tempscore; public Text ScoreText; //public Text myText; //int punkcik = 0; bool deleted = false; // Start is called before the first frame update void Start() { score = 0; /////////set score to 0 before anything /* SetScoreText(); punkty.text = "Punkty: " + score.ToString(); */ } // Update is called once per frame void Update() { if (Input.GetKeyDown(KeyCode.LeftArrow)) { transform.position += new Vector3(-1, 0, 0); //zmień pozycje w lewo if (ValidMove()) { transform.position -= new Vector3(-1, 0, 0); //sprawdź czy można się poruszyć } }//lewo ;go left else if (Input.GetKeyDown(KeyCode.RightArrow)) { transform.position += new Vector3(1, 0, 0); //zmień pozycje w prawo if (ValidMove()) { transform.position -= new Vector3(1, 0, 0); } }//prawo; go right else if (Input.GetKeyDown(KeyCode.UpArrow)) { transform.RotateAround(transform.TransformPoint(rotationPoint), new Vector3(0, 0, 1), 90); //obrót wokół własnej osi if (ValidMove()) { transform.RotateAround(transform.TransformPoint(rotationPoint), new Vector3(0, 0, 1), -90); } }//dół przyspieszenie; speed up down movement if (Time.time - previousTime > (Input.GetKey(KeyCode.DownArrow) ? fallTime / 10 : fallTime)) //down movement + line deleting { transform.position += new Vector3(0, -1, 0); if (ValidMove()) { transform.position -= new Vector3(0, -1, 0); AddToGrid(); CheckForLines(); this.enabled = false; FindObjectOfType<SpawnTetromino>().NewTetromino(); } previousTime = Time.time; }//ruch w dół + usunięcie linii; down movement + checking for line deleting if (deleted == true) // if line deleted then { score += 10; // add 10 points Debug.Log(score); // check score Debug.Log("usunieto? " + deleted); //check if bool deleted == true usunieto = false; // set bool deleted to false Debug.Log("usunieto set false? " + deleted); // check if it was succesfuly deleted } } void CheckForLines() { for (int i = height-1; i >= 0; i--) { if(HasLine(i)) { DeleteLine(i); RowDown(i); } } } bool HasLine(int i) // check if line is full; function also calles DeleteLine() in previous if statement { for (int j = 0; j < width; j++) { if (grid[j, i] == null) return false; } deleted = true; //set bool deleted = true as line is deleted return true; } void DeleteLine(int i) //delete line { for (int j = 0; j < width; j++) { Destroy(grid[j, i].gameObject); grid[j, i] = null; } } void RowDown(int i) { for (int y = i; y < height; y++) { for (int j = 0; j < width; j++) { if (grid[j,y] != null) { grid[j, y - 1] = grid[j, y]; grid[j, y] = null; grid[j, y - 1].transform.position -= new Vector3(0, 1, 0); } } } } // move undeleted blocks one row down void AddToGrid() // { foreach (Transform children in transform) { int roundedX = Mathf.RoundToInt(children.transform.position.x); //zaokrąglij pozycje X i Y int roundedY = Mathf.RoundToInt(children.transform.position.y); grid[roundedX, roundedY] = children; } } bool ValidMove() { foreach (Transform children in transform) { int roundedX = Mathf.RoundToInt(children.transform.position.x); //zaokrąglij pozycje X i Y int roundedY = Mathf.RoundToInt(children.transform.position.y); if (roundedX < 0 || roundedX >= width || roundedY < 0 || roundedY >= height) //sprawdź czy mieści się w polu gry { return true; } if (grid[roundedX, roundedY] != null) return true; } return false; } // check if the blocks can move }
And here is the place where all the points should be shown on the screen: using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; //using TetrisBlock.cs;
public class Punkty : MonoBehaviour
{
public Text myText;
public int punktypkt;
void Start()
{
GameObject.Find("Punctation").GetComponent<TetrisBlock>().score; // get component from TetrisBlock
punktypkt = 0;
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
punktypkt += 10;
myText.text = "Points: " + punktypkt.ToString();
Debug.Log(punktypkt);
}
}
}
Can someone please help me with this crap? Its sooo frustrating, Im trying to take the score from the other class with GameObject.Find("Punctation").GetComponent<TetrisBlock>().score;
but it just doesn work...
Answer by wiking333 · Jan 28, 2020 at 05:46 PM
Ok I dealt with the problem here is new code for the points: using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; //using TetrisBlock.cs;
public class Punkty : MonoBehaviour
{
public static int punktypkt = 0;
public Text punktyTXT;
void Start()
{
punktyTXT = GetComponent<Text>();
}
void Update()
{
punktyTXT.text = "Punkty: " + punktypkt;
Debug.Log(punktypkt);
}
}
And I also added this line "Punkty.punktypkt += 10;" in the other script where blocks are destroyed
And for the reference I watched this particular video on yt: https://www.youtube.com/watch?v=QbqnDbexrCw