Ok so Im making a gun system and for some reason keep getting the error(Object reference not set to an instance of an object). Im quite confused and am wondering if I did something wrong with my code (havnt done c# in a hot minute)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WeaponHolder : MonoBehaviour
{
public GameObject bullet;
private Transform weapon;
[SerializeField] LayerMask shootMask;
private CustomFunctions customFunctions; // Has our vec2 to vec3 func
void Update()
{
}
public void Shoot(float Damage, float Range, float BulletSpeed, Vector3 ShootPosition) //This is the line they say the problems on
{
//Shooting raycast and creating instantiating new bulllet
RaycastHit2D hit = Physics2D.Raycast(customFunctions.vec3tovec2(ShootPosition), transform.right, Range, shootMask);
BulletScript cloneBullet = Instantiate(bullet, ShootPosition, transform.rotation).GetComponent<BulletScript>();
//Bullet var setup
cloneBullet.bulletSpeed = BulletSpeed; cloneBullet.damage = Damage; cloneBullet.point = ShootPosition + transform.right * Range;
}
}
And the Im calliing the shoot function from another script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GunScript : MonoBehaviour
{
public GunData gun;
public Transform shootPoint;
public WeaponHolder weaponholder;
void Update()
{
//If mouse button is pressed call shoot with delay function
if (Input.GetMouseButton(0) && shotfinished)
{
StartCoroutine(shootWithDelay());
}
}
//Check wether gun is automatic or semi then call shoot value in weaponholder with gundata stats
void sendShoot()
{
if (gun.Automatic == true && Input.GetMouseButton(0))
{
weaponholder.Shoot(gun.Damage, gun.Range, gun.BulletSpeed, shootPoint.position);
}
if(gun.Automatic == false && Input.GetMouseButtonDown(0))
{
weaponholder.Shoot(gun.Damage, gun.Range, gun.BulletSpeed, shootPoint.position);
}
}
//Run sendShoot with shot speed
private bool shotfinished = true;
IEnumerator shootWithDelay()
{
sendShoot();
shotfinished = false;
yield return new WaitForSeconds(gun.ShootSpeed);
shotfinished = true;
}
}