Question by 
               xDark420 · Jul 29, 2021 at 07:21 PM · 
                uiunity 2drigidbody2dammo  
              
 
              [HELP] 2D Ammo UI error wont count down shooting count and wont pick up and change ammo count
NullReferenceException: Object reference not set to an instance of an object PlayerController.UpdateAmmoUI () (at Assets/Scripts/PlayerController.cs:132) PlayerController.Update () (at Assets/Scripts/PlayerController.cs:97) error in Unity console.
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class PlayerController : MonoBehaviour { public static PlayerController instance;
 public Rigidbody2D theRB;
 public float moveSpeed = 5f;
 private Vector2 moveInput;
 private Vector2 mouseInput;
 public float mouseSensitivity = 1f;
 public Camera viewCam;
 public GameObject bulletInpact;
 public int currentAmmo;
 public Animator gunAnim;
 public int currentHeath;
 public int maxHealth = 100;
 public GameObject deadScreen;
 private bool hasDied;
 public Text healthText, ammoText;
 private void Awake()
 {
     instance = this;
 }
 // Start is called before the first frame update
 void Start()
 {
     currentHeath = maxHealth;
     healthText.text = currentHeath.ToString() + "%";
     ammoText.text = currentAmmo.ToString();
 }
 // Update is called once per frame
 void Update()
 {
     if (!hasDied)
     {
     }
     //player movement
     moveInput = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
     Vector3 moveHorizontal = transform.up * -moveInput.x;
     Vector3 moveVertical = transform.right * moveInput.y;
     theRB.velocity = (moveHorizontal + moveVertical) * moveSpeed;
     //player view contol
     mouseInput = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y")) * mouseSensitivity;
     transform.rotation = Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, transform.rotation.eulerAngles.z - mouseInput.x);
     viewCam.transform.localRotation = Quaternion.Euler(viewCam.transform.localRotation.eulerAngles + new Vector3(0f, mouseInput.y, 0f));
     //Shooting
     if (Input.GetMouseButtonDown(0))
     {
         if (currentAmmo > 0)
         {
             Ray ray = viewCam.ViewportPointToRay(new Vector3(.5f, .5f, 0f));
             RaycastHit hit;
             if (Physics.Raycast(ray, out hit))
             {
                 //Debug.Log("I'm looking at " + hit.transform.name);
                 Instantiate(bulletInpact, hit.point, transform.rotation);
                 if (hit.transform.tag == "Enemy")
                 {
                     hit.transform.parent.GetComponent<EnemyController>().TakeDamage();
                 }
             }
             else
             {
                 Debug.Log("I'm looking at nothing!");
             }
             currentAmmo--;
             gunAnim.SetTrigger("Shoot");
             UpdateAmmoUI();
         }
     }
 }
 public void TakeDamage(int damageAmount)
 {
     currentHeath -= damageAmount;
     if(currentHeath <= 0)
     {
         deadScreen.SetActive(true);
         hasDied = true;
         currentHeath = 0;
     }
     healthText.text = currentHeath.ToString() + "%";
 }
 public void AddHealth(int healAmount)
 {
     currentHeath += healAmount;
     if(currentHeath > maxHealth)
     {
         currentHeath = maxHealth;
         healthText.text = currentHeath.ToString() + "%";
     }
 }
 public void UpdateAmmoUI()
 {
     ammoText.text = currentAmmo.ToString();
 }
}
               Comment
              
 
               
              Your answer
 
 
             Follow this Question
Related Questions
Collision sticking? 1 Answer
Rigidbody2D and Raycasting. Good practice? 0 Answers
Tower Defense 2D showing and hiding tower choose UI above building place 0 Answers
How to mask a Sprite Mask? 0 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                