Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to add save high score to existing script any help please?

Discussion in 'Scripting' started by 82COBRA82, May 10, 2014.

  1. 82COBRA82

    82COBRA82

    Joined:
    Sep 19, 2013
    Posts:
    3
    using UnityEngine;
    using System.Collections;
    using Holoville.HOTween;

    Code (csharp):
    1. /// <summary>
    2. ///  This class is the main entry point of the game it should be attached to a gameobject and be instanciate in the scene
    3. /// Author : Pondomaniac Games
    4. /// </summary>
    5. public class Main : MonoBehaviour {
    6.    
    7.     public GameObject _indicator;//The indicator to know the selected tile
    8.     public GameObject[,]  _arrayOfShapes;//The main array that contain all games tiles
    9.     private GameObject _currentIndicator;//The current indicator to replace and destroy each time the player change the selection
    10.     private GameObject _FirstObject;//The first object selected
    11.     private GameObject _SecondObject;//The second object selected
    12.     public GameObject [] _listOfGems;//The list of tiles we cant to see in the game you can remplace them in unity's inspector and choose all what you want
    13.     public GameObject _emptyGameobject;//After destroying object they are replaced with this one so we will replace them after with new ones
    14.     public GameObject _particleEffect;//The object we want to use in the effect of shining stars
    15.     public GameObject _particleEffectWhenMatch;//The gameobject of the effect when the objects are matching
    16.     public bool _canTransitDiagonally = false;//Indicate if we can switch diagonally
    17.     public int _scoreIncrement;//The amount of point to increment each time we find matching tiles
    18.     private int _scoreTotal = 0;//The score
    19.  
    20.     private ArrayList _currentParticleEffets= new ArrayList();//the array that will contain all the matching particle that we will destroy after
    21.     public AudioClip MatchSound;//the sound effect when matched tiles are found
    22.     public int _gridWidth;//the grid number of cell horizontally
    23.     public int _gridHeight;//the grid number of cell vertically
    24.    
    25.     // Use this for initialization
    26.     void Start () {
    27.         //Initializing the array with _gridWidth and _gridHeight passed in parameter
    28.         _arrayOfShapes = new GameObject[_gridWidth, _gridHeight];
    29.         //Creating the gems from the list of gems passed in parameter
    30.         for ( int i = 0; i <= _gridWidth-1; i++){
    31.             for ( int j = 0; j <= _gridHeight-1; j++){
    32.                 var gameObject = GameObject.Instantiate(_listOfGems[Random.Range(0, _listOfGems.Length)] as GameObject, new Vector3(i, j, 0), transform.rotation) as GameObject;
    33.                 _arrayOfShapes[i,j]= gameObject;
    34.              }
    35.         }
    36.         //Adding the star effect to the gems and call the DoShapeEffect continuously
    37.        InvokeRepeating("DoShapeEffect", 1f, 0.21F);
    38.  
    39.     }
    40.  
    41.     // Update is called once per frame
    42.     void Update () {
    43.  
    44.         bool shouldTransit = false;
    45.         //Detecting if the player clicked on the left mouse button and also if there is no animation playing
    46.         if (Input.GetButtonDown ("Fire1")  HOTween.GetTweenInfos()==null) {
    47.            
    48.             Destroy(_currentIndicator);
    49.             //The 3 following lines is to get the clicked GameObject and getting the RaycastHit2D that will help us know the clicked object
    50.             Ray ray   = Camera.main.ScreenPointToRay (Input.mousePosition);
    51.             RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
    52.             if (hit.transform!=null)
    53.             {  //To know if the user already selected a tile or not yet
    54.                if ( _FirstObject == null ) _FirstObject = hit.transform.gameObject;
    55.                else {_SecondObject=  hit.transform.gameObject;
    56.                     shouldTransit= true ; }
    57.  
    58.                 _currentIndicator =  GameObject.Instantiate (_indicator,new Vector3( hit.transform.gameObject.transform.position.x,hit.transform.gameObject.transform.position.y,-1), transform.rotation) as GameObject ;
    59.                 //If the user select the second tile we will swap the two tile and animate them
    60.                 if (shouldTransit)
    61.                 {
    62.                     //Getting the position between the 2 tiles
    63.                     var distance = _FirstObject.transform.position - _SecondObject.transform.position;
    64.                     //Testing if the 2 tiles are next to each others otherwise we will not swap them
    65.                     if (Mathf.Abs(distance.x) <= 1  Mathf.Abs(distance.y) <= 1)
    66.                     {   //If we dont want the player to swap diagonally
    67.                         if (!_canTransitDiagonally  )
    68.                         {
    69.                             if (distance.x != 0  distance.y != 0)
    70.                             {
    71.                                 Destroy(_currentIndicator);
    72.                                 _FirstObject = null;
    73.                                 _SecondObject = null;
    74.                                 return;
    75.                             }
    76.                         }
    77.                        //Animate the transition
    78.                         DoSwapMotion(_FirstObject.transform, _SecondObject.transform);
    79.                         //Swap the object in array
    80.                         DoSwapTile(_FirstObject, _SecondObject, ref _arrayOfShapes);
    81.  
    82.                                                
    83.                     }
    84.                     else
    85.                     {
    86.                         _FirstObject = null;
    87.                         _SecondObject = null;
    88.  
    89.                     }
    90.                    Destroy(_currentIndicator);
    91.                                        
    92.                 }
    93.                
    94.             }
    95.          
    96.                     }
    97.         //If no animation is playing
    98.         if ( HOTween.GetTweenInfos()==null)
    99.         {var Matches = FindMatch(_arrayOfShapes);
    100.             //If we find a matched tiles
    101.             if (Matches.Count > 0 )
    102.             {//Update the score
    103.                 _scoreTotal += Matches.Count * _scoreIncrement;
    104.                
    105.                 foreach (GameObject go in Matches) {
    106.                     //Playing the matching sound
    107.                     audio.PlayOneShot(MatchSound);
    108.                     //Creating and destroying the effect of matching
    109.                     var destroyingParticle = GameObject.Instantiate(_particleEffectWhenMatch as GameObject, new Vector3(go.transform.position.x, go.transform.position.y, -2), transform.rotation) as GameObject;
    110.                   Destroy(destroyingParticle, 1f);
    111.                     //Replace the matching tile with an empty one
    112.                     _arrayOfShapes[(int)go.transform.position.x, (int)go.transform.position.y] = GameObject.Instantiate(_emptyGameobject, new Vector3((int)go.transform.position.x, (int)go.transform.position.y, -1), transform.rotation) as GameObject;
    113.                    //Destroy the ancient matching tiles
    114.                     Destroy(go,0.1f); }
    115.                 _FirstObject = null;
    116.                 _SecondObject = null;
    117.                 //Moving the tiles down to replace the empty ones
    118.                 DoEmptyDown(ref _arrayOfShapes);
    119.             }  
    120.        //If no matching tiles are found remake the tiles at their places
    121.             else if (_FirstObject != null
    122.                       _SecondObject != null
    123.                      ){
    124.                 //Animate the tiles
    125.                 DoSwapMotion(_FirstObject.transform, _SecondObject.transform);
    126.                 //Swap the tiles in the array
    127.                 DoSwapTile(_FirstObject, _SecondObject, ref _arrayOfShapes);
    128.                 _FirstObject = null;
    129.                 _SecondObject = null;
    130.            
    131.         }
    132.         }
    133.         //Update the score
    134.         (GetComponent(typeof( TextMesh))as TextMesh).text = _scoreTotal.ToString();
    135.     }
    136.  
    137.     // Find Match-3 Tile
    138.     private ArrayList FindMatch(GameObject[,] cells)
    139.     {//creating an arraylist to store the matching tiles
    140.         ArrayList stack = new ArrayList();
    141.         //Checking the vertical tiles
    142.         for (var x = 0; x <= cells.GetUpperBound(0); x++)
    143.         {
    144.             for (var y = 0; y <= cells.GetUpperBound(1); y++)
    145.             {
    146.                 var thiscell = cells[x, y];
    147.                 //If it's an empty tile continue
    148.                 if (thiscell.name == "Empty(Clone)") continue;
    149.                 int matchCount = 0;
    150.                 int y2 = cells.GetUpperBound(1);
    151.                 int y1;
    152.                 //Getting the number of tiles of the same kind
    153.                 for (y1 = y + 1; y1 <= y2; y1++)
    154.                 {
    155.                    if (cells[x, y1].name == "Empty(Clone)" || thiscell.name != cells[x, y1].name) break;
    156.                     matchCount++;
    157.                 }
    158.                 //If we found more than 2 tiles close we add them in the array of matching tiles
    159.                 if (matchCount >= 2)
    160.                 {
    161.                     y1 = Mathf.Min(cells.GetUpperBound(1) , y1 - 1);
    162.                     for (var y3 = y; y3 <= y1; y3++)
    163.                     {
    164.                         if (!stack.Contains(cells[x, y3]))
    165.                         {
    166.                             stack.Add(cells[x, y3]);
    167.                         }
    168.                     }
    169.                 }
    170.             }
    171.         }
    172.         //Checking the horizontal tiles , in the following loops we will use the same concept as the previous ones
    173.         for (var y = 0; y < cells.GetUpperBound(1) + 1; y++)
    174.         {
    175.             for (var x = 0; x < cells.GetUpperBound(0) + 1; x++)
    176.             {
    177.                 var thiscell = cells[x, y];
    178.                 if (thiscell.name == "Empty(Clone)") continue;
    179.                 int matchCount = 0;
    180.                int x2 = cells.GetUpperBound(0);
    181.                 int x1;
    182.                 for (x1 = x + 1; x1 <= x2; x1++)
    183.                 {
    184.                     if (cells[x1, y].name == "Empty(Clone)" || thiscell.name != cells[x1, y].name) break;
    185.                     matchCount++;
    186.                 }
    187.                 if (matchCount >= 2)
    188.                 {
    189.                     x1 = Mathf.Min(cells.GetUpperBound(0), x1 - 1);
    190.                     for (var x3 = x; x3 <= x1; x3++)
    191.                     {
    192.                         if (!stack.Contains(cells[x3, y]))
    193.                         {
    194.                             stack.Add(cells[x3, y]);
    195.                          }
    196.                     }
    197.                 }
    198.             }
    199.         }
    200.         return stack;
    201.     }
    202.  
    203.     // Swap Motion Animation, to animate the switching arrays
    204.     void DoSwapMotion(Transform a, Transform b)
    205.     {
    206.         Vector3 posA = a.localPosition;
    207.         Vector3 posB = b.localPosition;
    208.         TweenParms parms = new TweenParms().Prop("localPosition", posB).Ease(EaseType.EaseOutQuart);
    209.         HOTween.To(a, 0.25f, parms).WaitForCompletion();
    210.         parms = new TweenParms().Prop("localPosition", posA).Ease(EaseType.EaseOutQuart);
    211.        HOTween.To(b, 0.25f, parms).WaitForCompletion();
    212.     }
    213.  
    214.  
    215.     // Swap Two Tile, it swaps the position of two objects in the grid array
    216.     void DoSwapTile(GameObject a, GameObject b, ref GameObject[,] cells)
    217.     {
    218.         GameObject cell = cells[(int)a.transform.position.x, (int)a.transform.position.y];
    219.         cells[(int)a.transform.position.x, (int)a.transform.position.y] = cells[(int)b.transform.position.x, (int)b.transform.position.y];
    220.         cells[(int)b.transform.position.x, (int)b.transform.position.y] = cell;
    221.     }
    222.  
    223.     // Do Empty Tile Move Down
    224.     private void DoEmptyDown(ref GameObject[,] cells)
    225.     {   //replace the empty tiles with the ones above
    226.         for (int x= 0; x <= cells.GetUpperBound(0); x++)
    227.         {
    228.             for (int y = 0; y <= cells.GetUpperBound(1); y++)
    229.             {
    230.  
    231.                 var thisCell = cells[x, y];
    232.                 if (thisCell.name == "Empty(Clone)")
    233.                 {
    234.  
    235.                     for (int y2 = y; y2 <= cells.GetUpperBound(1); y2++)
    236.                     {
    237.                         if (cells[x, y2].name != "Empty(Clone)")
    238.                         {
    239.                             cells[x, y] = cells[x, y2];
    240.                             cells[x, y2] = thisCell;
    241.                             break;
    242.                         }
    243.  
    244.                     }
    245.                
    246.                 }
    247.  
    248.             }
    249.         }
    250.         //Instantiate new tiles to replace the ones destroyed
    251.         for (int x = 0; x <= cells.GetUpperBound(0); x++)
    252.         {
    253.             for (int y = 0; y <= cells.GetUpperBound(1); y++)
    254.             {
    255.                 if (cells[x, y].name == "Empty(Clone)")
    256.                 {
    257.                     Destroy(cells[x, y]);
    258.                     cells[x, y] = GameObject.Instantiate(_listOfGems[Random.Range(0, _listOfGems.Length)] as GameObject, new Vector3(x, cells.GetUpperBound(1)+2, 0), transform.rotation) as GameObject;
    259.                
    260.             }
    261.             }
    262.         }
    263.  
    264.         for (int x = 0; x <= cells.GetUpperBound(0); x++)
    265.             {
    266.                 for (int y = 0; y <= cells.GetUpperBound(1) ; y++)
    267.                 {
    268.  
    269.                     TweenParms parms = new TweenParms().Prop("position", new Vector3(x, y, -1)).Ease(EaseType.EaseOutQuart);
    270.                     HOTween.To(cells[x, y].transform, .4f, parms);
    271.                 }
    272.             }
    273.  
    274.      
    275.      
    276.     }
    277.     //Instantiate the star objects
    278.       void DoShapeEffect()
    279.     {
    280.         foreach (GameObject row in _currentParticleEffets)
    281.         Destroy(row);
    282.         for (int i = 0; i <= 2; i++)
    283.             _currentParticleEffets.Add(GameObject.Instantiate(_particleEffect, new Vector3(Random.Range(0, _arrayOfShapes.GetUpperBound(0) + 1), Random.Range(0, _arrayOfShapes.GetUpperBound(1) + 1), -1), new Quaternion(0, 0, Random.Range(0, 1000f),100)) as GameObject);
    284.   }
    285.  
    286.    
    287. }
     
    Last edited: May 12, 2014
  2. FreshTillDeath0

    FreshTillDeath0

    Joined:
    Feb 18, 2013
    Posts:
    69
    Code (csharp):
    1.  
    2. Insert Code Here
    3.  
    Near impossible to read without getting a headache
     
  3. 82COBRA82

    82COBRA82

    Joined:
    Sep 19, 2013
    Posts:
    3
    how to do that iam new to this forum thanks
     
    Last edited: May 12, 2014
  4. Bivrost

    Bivrost

    Joined:
    Mar 26, 2014
    Posts:
    80
    OMG, please study this thread about How To Use Code Tags thoroughly first before posting anything else again. You can also edit your original post, there's no need to re-post megabytes of unreadable code.
     
  5. 82COBRA82

    82COBRA82

    Joined:
    Sep 19, 2013
    Posts:
    3
    Thanks i got it.