So, I have 4 scripts : GunInfo(where i store some information about gun entity uses), WeaponInfo(where i store some additional info about weapon entity in my game uses), PlayerShooting(where I instantiate my bullet and attempt to store that GunInfo to be accesed by the bullet in WeaponInfo script) and lastly BulletMovement(where I implement bullet movement based on that GunInfo script that I stored in WeaponInfo script).
Here are the scripts:
using UnityEngine;
public class GunInfo : MonoBehaviour
{
[SerializeField] private int magazine, bulletCount;
[SerializeField] private float bulletSpeed, reloadTime, flyTime;
public int Magazine { get; private set; }
public int BulletCount { get; private set; }
public float BulletSpeed { get; private set; }
public float ReloadTime { get; private set; }
public float BulletFlyTime { get; private set; }
private void Awake()
{
Magazine = magazine;
BulletCount = bulletCount;
BulletSpeed = bulletSpeed;
ReloadTime = reloadTime;
BulletFlyTime = flyTime;
}
public void Reload()
{
var reloadBullets = Mathf.Min(BulletCount, magazine);
Debug.Log("Reloaded " + reloadBullets + " bullets");
BulletCount -= reloadBullets;
Magazine += reloadBullets;
}
public void HadShot()
{
Magazine--;
}
}
using UnityEngine;
public class WeaponInfo : MonoBehaviour
{
[SerializeField] private int startingDamage;
[SerializeField] private bool destroyable;
[SerializeField] private User user;
public enum User { Ally, Enemy }
public User UserName { get; private set; }
public int Damage { get; private set; }
public bool Destroyable { get; private set; }
public GunInfo Gun { get; private set; }
private void Awake()
{
Damage = startingDamage;
Destroyable = destroyable;
UserName = user;
}
public void UsedGun(GunInfo gun)
{
Gun = gun;
}
}
using System.Collections;
using UnityEngine;
public class PlayerShooting : MonoBehaviour
{
[SerializeField] private float fireRate;
[SerializeField] private GameObject bullet;
private float lastShot = 0f;
private GunInfo gunInfo;
private bool isReloading = false;
private void Awake()
{
gunInfo = GetComponent<GunInfo>();
if(gunInfo == null)
{
Debug.Log("Can't find weapon");
}
}
private void Start()
{
UIManager.Instance.UpdateBullets(gunInfo.Magazine, gunInfo.BulletCount);
}
private void Update()
{
if (Input.GetMouseButtonUp(0))
{
Fire();
}
}
private void Fire()
{
if((gunInfo.BulletCount == 0 && gunInfo.Magazine == 0) || isReloading)
{
return;
}
else
{
if (gunInfo.Magazine == 0)
{
StartCoroutine(ReloadCoroutine());
}
else if (Time.time > lastShot + fireRate)
{
var bulletObj = Instantiate(bullet, transform.position, transform.rotation);
bulletObj.GetComponent<WeaponInfo>().UsedGun(gunInfo);
if(bulletObj.GetComponent<WeaponInfo>().Gun == null)
{
Debug.Log("No");
}
lastShot = Time.time;
gunInfo.HadShot();
}
UIManager.Instance.UpdateBullets(gunInfo.Magazine, gunInfo.BulletCount);
}
}
private IEnumerator ReloadCoroutine()
{
isReloading = true;
yield return new WaitForSeconds(gunInfo.ReloadTime);
gunInfo.Reload();
Debug.Log("Reloaded");
UIManager.Instance.UpdateBullets(gunInfo.Magazine, gunInfo.BulletCount);
isReloading = false;
}
}
using UnityEngine;
public class BulletMovement : MonoBehaviour
{
float timer = 0f;
private GunInfo gunInfo;
private void Awake()
{
gunInfo = GetComponent<WeaponInfo>().Gun;
}
private void Update()
{
if(timer <= gunInfo.BulletFlyTime)
{
Debug.Log("Here");
transform.position += gunInfo.BulletSpeed * Time.deltaTime * transform.right;
}
else Destroy(gameObject);
timer += Time.deltaTime;
}
}
Error occurs on line 15 in the BulletMovement script:
if(timer <= gunInfo.BulletFlyTime),
which means that gunInfo is null.
Furthermore, I identifed that each GetComponent works properly and lastly I realised that the Gun property on line 10:
gunInfo = GetComponent<WeaponInfo>().Gun;
wasnt accesed correctly. I cant seem to find out why it is null.