Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by wiking333 · Jan 27, 2020 at 10:22 AM · gameobjectgamesystemgameplaygameobject.find

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...

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

288 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

I need help with boss design. I am new to coding so any help would be nice. 0 Answers

Jacks game Simulator 0 Answers

Addressables' references - right object is there, but not! 1 Answer

how make object follow mouse cursor in 3d space 0 Answers

Can't find my Camera inside my Instantiate prefab 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges