Search Unity

SetInt to a fixed value?

Discussion in '2D' started by RuneShiStorm, Apr 3, 2021.

  1. RuneShiStorm

    RuneShiStorm

    Joined:
    Apr 28, 2017
    Posts:
    264
    Hi!
    I'm having trouble taking away ALL bullets from the player in my script and leave him with 1 Bullet.
    The way my bullets script work so far is this:

    GunScript
    Code (csharp):
    1.  
    2.  
    3.         if (controls.Player.Fire.triggered && canShoot)
    4.         {
    5.  
    6.             if (PlayerPrefs.GetInt("handGunBulletTemp") >= 1)
    7.             {
    8.                 HandGunShoot();
    9.             }
    10.         }
    11.  
    12.     public void HandGunShoot()
    13.     {
    14.         GameManager.Instance.minusHandGunBullet();
    15.     }
    16.  
    17.  
    GameManager
    Code (csharp):
    1.  
    2.  
    3.     public int handGunBullet;
    4.     public Text handGunBulletText;
    5.  
    6.     public void minusHandGunBullet()//Called from GunScript
    7.     {
    8.     PlayerPrefs.SetInt("handGunBulletTemp", PlayerPrefs.GetInt("handGunBulletTemp") - 1); // Minus one bullet.
    9.     handGunBulletText.text = PlayerPrefs.GetInt("handGunBulletTemp").ToString(); //Update the text
    10.     }
    11.  
    12.  
    Quite simple but I want to press a button and no matter how many bullets the player have, he will only be left with one bullet... Is that possible?

    Thankful for any advice or help!
     
  2. rarac

    rarac

    Joined:
    Feb 14, 2021
    Posts:
    570
    its hard to understand what you want but if i take your words literally:

    Code (CSharp):
    1.   if (Input.GetKeyDown(KeyCode.U))
    2.         {
    3.          PlayerPrefs.SetInt("handGunBulletTemp", 1);
    4.  
    5.         }
     
    RuneShiStorm likes this.
  3. RuneShiStorm

    RuneShiStorm

    Joined:
    Apr 28, 2017
    Posts:
    264
    ^
    Thats exacly what I was looking for! I tried, =< 1, <= 1, == 1 -+ 1 and all types of combos... Stupid me >_<