I do not know what this means
NullReferenceException: Object reference not set to an instance of an object
RhinoRunnerGaming.Weapon.RefreshAmmo (UnityEngine.UI.Text p_text) (at Assets/scripts/Weapon.cs:189)
RhinoRunnerGaming.Player.Update () (at Assets/scripts/Player.cs:126)
here is my player code
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine;
using Photon.Pun;
namespace RhinoRunnerGaming
{
public class Player : MonoBehaviourPunCallbacks
{
#region Variables
public float speed;
public float sprintModifier;
public Camera playerCamera;
public GameObject cameraParent;
public float jumpForce;
public int maxHealth;
public Transform weaponParent;
public Transform groundDetector;
public LayerMask ground;
private Transform ui_healthbar;
private Text ui_ammo;
private Rigidbody rig;
private Vector3 targetWeaponBobPosition;
private Vector3 weaponParentOrgin;
private float movementCounter;
private float idleCounter;
private float baseFOV;
private float sprintFOVModifier = 1.5f;
private int currentHealth;
private Manager manager;
private Weapon weapon;
#endregion
#region Monobehaivier Callbacks
void Start()
{
manager = GameObject.Find("Manager").GetComponent<Manager>();
weapon = GetComponent<Weapon>();
currentHealth = maxHealth;
if (photonView.IsMine)
{
gameObject.layer = 8;
}
cameraParent.SetActive(photonView.IsMine);
baseFOV = playerCamera.fieldOfView;
rig = GetComponent<Rigidbody>();
weaponParentOrgin = weaponParent.localPosition;
if (photonView.IsMine)
{
ui_healthbar = GameObject.Find("HUD/Health/Bar").transform;
ui_ammo = GameObject.Find("HUD/Ammo/Text").GetComponent<Text>();
RefreshHealthBar();
}
}
private void Update()
{
//Axis
float t_hmove = Input.GetAxisRaw("Horizontal");
float t_vmove = Input.GetAxisRaw("Vertical");
//Controls
bool sprint = Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift);
bool jump = Input.GetKeyDown(KeyCode.Space);
//States
bool isGrounded = Physics.Raycast(groundDetector.position, Vector3.down, 1f, ground);
bool isJumping = jump && isGrounded;
bool isSprinting = sprint && t_vmove > 0 && !jump;
//Jumping
if (jump && isGrounded)
{
rig.AddForce(Vector3.up * jumpForce);
}
if (Input.GetKeyDown(KeyCode.U))
{
TakeDamage(100);
}
//Head Bob
if (t_hmove == 0 && t_vmove == 0)
{
HeadBob(idleCounter, 0.025f, 0.025f);
idleCounter += Time.deltaTime;
weaponParent.localPosition = Vector3.Lerp(weaponParent.localPosition, targetWeaponBobPosition, Time.deltaTime * 2f);
}
else if (!isSprinting)
{
HeadBob(movementCounter, 0.035f, 0.035f);
movementCounter += Time.deltaTime * 3f;
weaponParent.localPosition = Vector3.Lerp(weaponParent.localPosition, targetWeaponBobPosition, Time.deltaTime * 6f);
}
else
{
HeadBob(movementCounter, 0.15f, 0.075f);
movementCounter += Time.deltaTime * 7f;
weaponParent.localPosition = Vector3.Lerp(weaponParent.localPosition, targetWeaponBobPosition, Time.deltaTime * 10f);
}
// UI Refreshes
RefreshHealthBar();
weapon.RefreshAmmo(ui_ammo);
}
void FixedUpdate()
{
if (!photonView.IsMine)
{
return;
}
//Axis
float t_hmove = Input.GetAxisRaw("Horizontal");
float t_vmove = Input.GetAxisRaw("Vertical");
//Controls
bool sprint = Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift);
bool jump = Input.GetKeyDown(KeyCode.Space);
//States
bool isGrounded = Physics.Raycast(groundDetector.position, Vector3.down, 1f, ground);
bool isJumping = jump && isGrounded;
bool isSprinting = sprint && t_vmove > 0 && !jump;
//Movement
Vector3 t_direction = new Vector3(t_hmove, 0, t_vmove);
t_direction.Normalize();
float t_adjustedSpeed = speed;
if (isSprinting) t_adjustedSpeed *= sprintModifier;
Vector3 t_targetVelocity = transform.TransformDirection(t_direction) * t_adjustedSpeed * Time.deltaTime;
t_targetVelocity.y = rig.velocity.y;
rig.velocity = t_targetVelocity;
//Field of View
if (isSprinting)
{
playerCamera.fieldOfView = Mathf.Lerp(playerCamera.fieldOfView, baseFOV * sprintFOVModifier, Time.deltaTime * 8);
}
else
{
playerCamera.fieldOfView = Mathf.Lerp(playerCamera.fieldOfView, baseFOV, Time.deltaTime * 8);
}
}
#endregion
#region Private Methods
void RefreshHealthBar()
{
float t_health_ratio = (float)currentHealth / (float)maxHealth;
ui_healthbar.localScale = Vector3.Lerp(ui_healthbar.localScale, new Vector3(t_health_ratio, 1, 1), Time.deltaTime * 8f);
}
void HeadBob(float p_z, float p_x_intensity, float p_y_intensity)
{
targetWeaponBobPosition = weaponParent.localPosition = weaponParentOrgin + new Vector3(Mathf.Cos(p_z) * p_x_intensity, Mathf.Sin(p_z * 2) * p_y_intensity, 0);
}
#endregion
#region Public Methods
public void TakeDamage (int p_damage)
{
if (photonView.IsMine)
{
currentHealth -= p_damage;
RefreshHealthBar();
Debug.Log(currentHealth);
if(currentHealth <= 0)
{
manager.Spawn();
PhotonNetwork.Destroy(gameObject);
}
}
}
#endregion
}
}
here is my weapon code
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine;
using Photon.Pun;
namespace RhinoRunnerGaming
{
public class Weapon : MonoBehaviourPunCallbacks
{
#region Variables
public Gun[] loadout;
public Transform weaponParent;
public GameObject bulletHolePrefab;
public LayerMask canBeShot;
private float currentCooldown;
private int currentIndex;
private GameObject currentWeapon;
private bool isReloading;
#endregion
#region Monobehavior Callbacks
private void Start()
{
foreach (Gun a in loadout) a.Initialize();
Equip(0);
}
void Update()
{
if (photonView.IsMine && Input.GetKeyDown(KeyCode.Alpha1))
{
photonView.RPC("Equip", RpcTarget.All, 0);
}
if (currentWeapon != null)
{
if (photonView.IsMine)
{
Aim(Input.GetMouseButton(1));
if (Input.GetMouseButtonDown(0) && currentCooldown <= 0)
{
if (loadout[currentIndex].FireBullet())
{
photonView.RPC("Shoot", RpcTarget.All);
}
else
{
loadout[currentIndex].Reload();
}
}
if (Input.GetKeyDown(KeyCode.R))
{
StartCoroutine(Reload(loadout[currentIndex].reload));
}
//CoolDown
if (currentCooldown > 0)
{
currentCooldown -= Time.deltaTime;
}
}
//weapon position eleasticity
currentWeapon.transform.localPosition = Vector3.Lerp(currentWeapon.transform.localPosition, Vector3.zero, Time.deltaTime * 4f);
}
}
#endregion
#region Private Methods
IEnumerator Reload(float p_wait)
{
isReloading = true;
currentWeapon.SetActive(false);
yield return new WaitForSeconds(p_wait);
loadout[currentIndex].Reload();
currentWeapon.SetActive(true);
isReloading = false;
}
[PunRPC]
void Equip(int p_ind)
{
if (currentWeapon != null)
{
if(isReloading) StopCoroutine("Reload");
Destroy(currentWeapon);
}
currentIndex = p_ind;
GameObject t_newWeapon = Instantiate(loadout[p_ind].prefab, weaponParent.position, weaponParent.rotation, weaponParent) as GameObject;
t_newWeapon.transform.localPosition = Vector3.zero;
t_newWeapon.transform.localEulerAngles = Vector3.zero;
t_newWeapon.GetComponent<Sway>().isMine = photonView.IsMine;
currentWeapon = t_newWeapon;
}
void Aim(bool p_isAiming)
{
Transform t_anchor = currentWeapon.transform.Find("anchor");
Transform t_state_ads = currentWeapon.transform.Find("states/ADS");
Transform t_state_hip = currentWeapon.transform.Find("states/hips");
if (p_isAiming)
{
//aim
t_anchor.position = Vector3.Lerp(t_anchor.position, t_state_ads.position, Time.deltaTime * loadout[currentIndex].aimspeed);
}
else
{
//hip
t_anchor.position = Vector3.Lerp(t_anchor.position, t_state_hip.position, Time.deltaTime * loadout[currentIndex].aimspeed);
}
}
[PunRPC]
void Shoot()
{
Transform t_spawn = transform.Find("camera/playerCamera");
//gun fx
currentWeapon.transform.Rotate(-loadout[currentIndex].recoil, 0, 0);
currentWeapon.transform.position -= currentWeapon.transform.forward * loadout[currentIndex].kickback;
//setup bloom
Vector3 t_bloom = t_spawn.position + t_spawn.forward * 1000f;
t_bloom += Random.Range(-loadout[currentIndex].bloom, loadout[currentIndex].bloom) * t_spawn.up;
t_bloom += Random.Range(-loadout[currentIndex].bloom, loadout[currentIndex].bloom) * t_spawn.right;
t_bloom -= t_spawn.position;
t_bloom.Normalize();
//raycast
RaycastHit t_hit = new RaycastHit();
if (Physics.Raycast(t_spawn.position, t_bloom, out t_hit, 1000f, canBeShot))
{
GameObject t_newHole = Instantiate(bulletHolePrefab, t_hit.point + t_hit.normal * 0.001f, Quaternion.identity) as GameObject;
t_newHole.transform.LookAt(t_hit.point + t_hit.normal);
Destroy(t_newHole, 5f);
if (photonView.IsMine)
{
//Shootin other player on network
if (t_hit.collider.gameObject.layer == 11)
{
t_hit.collider.gameObject.GetPhotonView().RPC("TakeDamage", RpcTarget.All, loadout[currentIndex].damage);
}
}
}
//cooldown
currentCooldown = loadout[currentIndex].firerate;
}
[PunRPC]
private void TakeDamage (int p_damage)
{
GetComponent<Player>().TakeDamage(p_damage);
}
#endregion
#region Public Methods
public void RefreshAmmo(Text p_text)
{
int t_clip = loadout[currentIndex].GetClip();
int t_stache = loadout[currentIndex].GetStash();
p_text.text = t_clip.ToString("D2") + " / " + t_stache.ToString("D2");
}
#endregion
}
}