Search Unity

C# Integer across 2d sprites

Discussion in '2D' started by unity_SLtsa6szbzDdtA, Dec 15, 2017.

  1. unity_SLtsa6szbzDdtA

    unity_SLtsa6szbzDdtA

    Joined:
    Dec 15, 2017
    Posts:
    1
    Hello all,

    I am trying to create a 2d game where right now I have 3 sprites every time you click the sprite it takes one away from an integer however when you click on one it counts down from 3 to 2 then if I click the other one it counts from 3 to 2 again.

    Code:
    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class Lives : MonoBehaviour
    7. {
    8.  
    9.     //When I want to take one off numberOfLives I just type 'numberOfLives = takeAwayLives(numberOfLives);'
    10.  
    11.     public BoxCollider2D BoxCollider2D;
    12.     public GameObject Cat1;
    13.     public GameObject Cat2;
    14.     public GameObject Cat3;
    15.     int numberOfLives = 3;
    16.     public GameObject Splat;
    17.  
    18.     void Start()
    19.     {
    20.         Debug.Log (numberOfLives);
    21.         Splat.gameObject.GetComponent<SpriteRenderer>().enabled = false;
    22.         this.gameObject.GetComponent<SpriteRenderer>().enabled = true;
    23.     }
    24.  
    25.     public int takeAwayLives(int number)
    26.     {
    27.         int ret;
    28.         ret = number - 1;
    29.         return ret;
    30.  
    31.     }
    32.  
    33.     void OnMouseDown()
    34.     {
    35.  
    36.         if(numberOfLives > 0)
    37.         {
    38.             numberOfLives = takeAwayLives(numberOfLives);
    39.             Debug.Log(numberOfLives);
    40.             StartCoroutine(afterClick());
    41.         }
    42.     }
    43.  
    44.     IEnumerator afterClick()
    45.     {
    46.         this.GetComponent<Animation>().Play("Splat");
    47.         this.BoxCollider2D.enabled = false;
    48.         yield return new WaitForSeconds(1);
    49.         if (numberOfLives == 2)
    50.         {
    51.             Cat1.gameObject.GetComponent<SpriteRenderer>().enabled = true;
    52.         }
    53.         if (numberOfLives == 1)
    54.         {
    55.             Cat2.gameObject.GetComponent<SpriteRenderer>().enabled = true;
    56.         }
    57.         if (numberOfLives == 0)
    58.         {
    59.             Cat3.gameObject.GetComponent<SpriteRenderer>().enabled = true;
    60.         }
    61.         this.gameObject.GetComponent<SpriteRenderer>().enabled = false;
    62.         this.gameObject.GetComponent<SpriteRenderer>().enabled = false;
    63.         Splat.gameObject.GetComponent<SpriteRenderer>().enabled = true;
    64.         yield return new WaitForSeconds(0.2f);
    65.         Splat.GetComponent<Animation>().Play("Splat Fade OUt");
    66.         yield return new WaitForSeconds(2f);
    67.         Splat.gameObject.GetComponent<SpriteRenderer>().enabled = false;
    68.     }
    69. }
    70.  
    Any help would be appreciated

    Thanks
     
  2. Amon

    Amon

    Joined:
    Oct 18, 2009
    Posts:
    1,384
    Every Instance of your class 'Lives' gives each instance 3 lives. That's why each sprite instance of class 'Lives' has 3 lives.
     
  3. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    ...And to fix that, you might simply put the keyword "static" in front of "int numberOfLives = 3".