• 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 franklin22 · Mar 12, 2017 at 11:19 PM · score

Where to put my score here??

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class Tile
 {
     public GameObject tileObj;
     public string type;
     public Tile (GameObject obj, string t)
     {
         tileObj = obj;
         type = t;
     }
 }
 public class CreateGame : MonoBehaviour 
 {
 
     public int scoreValu = 20;
 
 
     GameObject tile1 = null;
     GameObject tile2 = null;
 
     public GameObject [] tile;
     List<GameObject> tileBank = new List <GameObject> ();
 
     static int rows = 8;
     static int cols = 7;
     bool renewBoard = false;
     Tile[,] tiles = new Tile[cols,rows];
 
     void ShuffleList()
     {
         System.Random rand = new System.Random ();
         int r = tileBank.Count;
         while (r > 1){
             r--; 
             int n = rand.Next (r + 1);
             GameObject val = tileBank[n];
             tileBank [n] = tileBank [r];
             tileBank [r] = val;
         }
 
     }
         
 
     // Use this for initialization
     void Start () 
     {
 
         int numCopies = (rows * cols)/3;
         for (int i = 0; i < numCopies; i++)
         {
             for (int j = 0; j < tile.Length; j++) 
             {
                 GameObject o = (GameObject)Instantiate (tile [j],
                     new Vector3 (-10, -10, 0),
                     tile [j].transform.rotation);
                 
                 o.SetActive (false);
                 tileBank.Add(o);
             }
         }
          
         ShuffleList ();
         // initialize tile grid
         for (int r = 0; r < rows; r++)
             
         {
             for (int c = 0; c < cols; c++) 
             {
                 Vector3 tilePos = new Vector3 (c, r, 0);
                 for (int n = 0; n < tileBank.Count; n++)
                             
                 {
                     GameObject o = tileBank [n];
                     if (!o.activeSelf)
                         
                     {
                         o.transform.position = 
                             new Vector3 (tilePos.x,
                                             tilePos.y,
                                             tilePos.z);
                         
                         o.SetActive (true);
 
                         tiles [c, r] = new Tile (o, o.name);
                         n = tileBank.Count + 1;
 
                     }
                 }
             }
         }
     }
 
     // Update is called once per frame
     void Update ()
     {
         CheckGrid ();
         if (Input.GetMouseButtonDown (0))
         {
             Ray ray = Camera.main.ScreenPointToRay
                     (Input.mousePosition);
             RaycastHit2D hit =
                 Physics2D.GetRayIntersection (ray, 1000);
             if (hit)     
             {
                 tile1 = hit.collider.gameObject;
 
             }
         } 
         // if finger up is detected after
         // an initial tile has been chosen
         else if (Input.GetMouseButtonUp(0) && tile1)
         {
             Ray ray = Camera.main.ScreenPointToRay
                     (Input.mousePosition);
             RaycastHit2D hit = 
                 Physics2D.GetRayIntersection (ray, 1000);
             if (hit) 
             {
                 tile2 = hit.collider.gameObject;
             }
             if (tile1 && tile2) 
             {
                 int horzDist = (int)
                     Mathf.Abs (tile1.transform.position.x -
                         tile2.transform.position.x);
                 int vertDist = (int)
                     
                     Mathf.Abs (tile1.transform.position.y -
                         tile2.transform.position.y);
                 if (horzDist == 1 ^ vertDist == 1) 
                 {
 
                     Tile temp = tiles [(int)tile1.transform.position.x,
                                         (int)tile1.transform.position.y];
                                     tiles [(int)tile1.transform.position.x,
                                              (int)tile1.transform.position.y] = 
                     tiles [(int)tile2.transform.position.x,
                             (int)tile2.transform.position.y];
                     tiles [(int)tile2.transform.position.x,
                             (int)tile2.transform.position.y] = temp;
                         
                     Vector3 temPos = tile1.transform.position;
                     tile1.transform.position = 
                         tile2.transform.position;
                     tile2.transform.position = temPos;
                     //reset the touched tiles
 
                     tile1 = null;
                     tile2 = null;
                 }
                 else 
                 {
 
                 }
 
             }
         }
 
     }
 
     void CheckGrid()
     {
         int counter = 1;
         //check in the columns
 
         for (int r = 0; r < rows; r++)
         {
             counter = 1;
             for (int c = 1; c < cols; c++)
             {
                 if (tiles[c, r] != null && tiles[c - 1, r] != null)
                     //if the tiles exist
 
                 {
                     if (tiles [c, r].type == tiles [c - 1, r].type)
                     {
                         counter++;
                     }
                     else
                         counter = 1;//reset counter
                     //if there are found remove them
                     if (counter == 3)
                     {
                         if (tiles [c, r] != null)
                             tiles [c, r].tileObj.SetActive (false);
                         if (tiles [c-1, r] != null)
                             tiles [c-1, r].tileObj.SetActive
                             (false);
                         if (tiles [c-2, r] != null)
                             tiles [c-2, r].tileObj.SetActive
                             (false);
                         tiles [c, r] = null;
                         tiles [c-1, r] = null;
                         tiles [c-2, r] = null;    
                         renewBoard = true;
                     }
                 }
             }
         }
 
 
         for (int c = 0; c < cols; c++)
         {
             counter = 1;
             for (int r = 1; r < rows; r++) {
                 if (tiles [c, r] != null && tiles [c, r-1]
                                                 != null) 
                 {
                     if (tiles [c, r].type == tiles[c, r-1].type)
                     {
                         counter++;
                     } 
                     else 
                         counter = 1;//reset counter
                     //if three are found remove them
                     if (counter == 3) 
                     {
                         if (tiles [c, r] != null)
                             tiles [c, r].tileObj.SetActive
                             (false);
                         if (tiles [c, r-1] != null)
                             tiles [c, r-1].tileObj.SetActive
                             (false);
                         if (tiles [c, r-2] != null)
                             tiles [c, r-2].tileObj.SetActive
                             (false);
                         tiles [c, r] = null;
                         tiles [c, r -1] = null;
                         tiles [c, r -2] = null;
                         renewBoard = true;
 
                     }
                 }
             }
         }
         if (renewBoard)
         { 
             RenewGrid();
             renewBoard = false;
         }
     }
 
     void RenewGrid()
     {
         bool anyMoved = false;
         ShuffleList ();
         for (int r = 1; r < rows; r++)
         {
             for (int c = 0; c < cols; c++)
             {
                 if (r == rows-1 && tiles [c,r] == null)
                 {                    //if in the top row  and no tile
                     Vector3 tilePos = new Vector3 (c, r, 0);
                     for (int n = 0; n < tileBank.Count; n++) 
                     {
                         GameObject o = tileBank [n];
                         if (!o.activeSelf) 
                         {
                             o.transform.position = new
                                 Vector3 (tilePos.x, tilePos.y,
                                     tilePos.z);
                             o.SetActive (true);
                             tiles [c, r] = new Tile (o, o.name);
                             n = tileBank.Count + 1;
                         }
                     }
                 }
                 if (tiles [c, r] != null)
                 {
                     //drop down if space below is empty
                     if (tiles [c, r - 1] == null) 
                     {
                         tiles [c, r - 1] = tiles [c, r];
                         tiles [c, r - 1].tileObj.transform.
                         position = new Vector3 (c, r-1, 0);
                         tiles [c, r] = null;
                         anyMoved = true;
                     }
                 }
             }
         }
         if (anyMoved) 
         {
             Invoke ("RenewGrid", 0.5f);
         }
 
     }
 }
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

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

94 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

Related Questions

How to reset GUI score after falling down? 4 Answers

For Loops Situation 1 Answer

Score Points based on Animating Png Texture 2 Answers

Score points when player pulls enemy up to certain area ? 0 Answers

Problem with ScoreBoard in Ping Pong game 1 Answer


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