my script cannot use the find method or it cant find the CameraRecoil/CameraRot so i dont know what to do
8730681–1181400–GunScript.cs (4.4 KB)
my script cannot use the find method or it cant find the CameraRecoil/CameraRot so i dont know what to do
8730681–1181400–GunScript.cs (4.4 KB)
my code :
using UnityEngine;
using TMPro;
public class GunScript : MonoBehaviour
{
//BULLET
public GameObject bullet;
//Ads Speed
public float adsspeed = 18f;
//Bullet force
public float shootForce, upwardForce;
//Gun stats
public float timeBetweenShooting, spread, reloadTime, timeBetweenShots;
public int magazineSize, bulletsPerTap;
public bool allowButtonHold;
int bulletsLeft, bulletsShot;
//bools
bool shooting, readyToShoot, reloading;
//Reference
public Camera fpsCam;
public Transform attackPoint;
//Ads In general
public Vector3 originalPosition;
public Vector3 aimPosition;
public bool allowInvoke = true;
private Recoil Recoil_Script;
public void Start()
{
Recoil_Script = transform.Find(“CameraRot/CameraRecoil”).GetComponent();
}
private void Awake()
{
//make sure magizine is full
bulletsLeft = magazineSize;
readyToShoot = true;
}
private void Update()
{
AimDownSightes();
MyInput();
}
private void MyInput()
{
//Check if allowed to hold down button and take corresponding input
if (allowButtonHold) shooting = Input.GetKey(KeyCode.Mouse0);
else shooting = Input.GetKeyDown(KeyCode.Mouse0);
//Reloading
if (Input.GetKeyDown(KeyCode.R) && bulletsLeft < magazineSize && !reloading) Reload();
//Shooting
if (readyToShoot && shooting && !reloading && bulletsLeft > 0)
{
//Set bullets shot to 0
bulletsShot = 0;
Shoot();
}
}
public void Shoot()
{
readyToShoot = false;
//Find the exact hit position using a raycast
Ray ray = fpsCam.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0)); //Just a ray through the middle of your current view
RaycastHit hit;
//check if ray hits something
Vector3 targetPoint;
if (Physics.Raycast(ray, out hit))
targetPoint = hit.point;
else
targetPoint = ray.GetPoint(75); //Just a point far away from the player
//Calculate direction from attackPoint to targetPoint
Vector3 directionWithoutSpread = targetPoint - attackPoint.position;
//Calculate spread
float x = Random.Range(-spread, spread);
float y = Random.Range(-spread, spread);
//Calculate new direction with spread
Vector3 directionWithSpread = directionWithoutSpread + new Vector3(x, y, 0); //Just add spread to last direction
//Instantiate bullet/projectile
GameObject currentBullet = Instantiate(bullet, attackPoint.position, Quaternion.identity);
//Rotate bullet to shoot direction
currentBullet.transform.forward = directionWithSpread.normalized;
//Add forces to bullet
currentBullet.GetComponent().AddForce(directionWithSpread.normalized * shootForce, ForceMode.Impulse);
currentBullet.GetComponent().AddForce(fpsCam.transform.up * upwardForce, ForceMode.Impulse);
bulletsLeft–;
bulletsShot++;
//Invoke resetShot function ( if not already invoked )
if (allowInvoke)
{
Invoke(“ResetShot”, timeBetweenShooting);
allowInvoke = false;
}
//if more than one bullet bulletsPerTap make sure to repeat shoot function
if (bulletsShot < bulletsPerTap && bulletsLeft > 0)
Invoke(“Shoot”, timeBetweenShots);
Recoil_Script.RecoilFire();
}
private void ResetShot()
{
//Allow shooting and invoke again
readyToShoot = true;
allowInvoke = true;
}
private void Reload()
{
reloading = true;
Invoke(“ReloadFinished”, reloadTime);
}
private void ReloadFinished()
{
bulletsLeft = magazineSize;
reloading = false;
}
private void AimDownSightes()
{
if (Input.GetButton(“Fire2”))
{
transform.localPosition = Vector3.Lerp(transform.localPosition, aimPosition, Time.deltaTime * adsspeed);
}
else
{
transform.localPosition = Vector3.Lerp(transform.localPosition, originalPosition, Time.deltaTime * adsspeed);
}
}
}
We don’t see any line numbers on your code do you? But does it seem reasonable to believe you have used the Find incorrectly and/or that isn’t the path to the file? Perhaps you should just make it public and assign it in the inspector.
No need to mis-post code. The answer is always the same.
How to fix a NullReferenceException error
https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/
Three steps to success:
I already know what s null it is this line of code :
Recoil_Script = transform.Find(“CameraRot/CameraRecoil”).GetComponent();
the stupid code cannot find CameraRot/CameraRecoil
i solved it i manually assigned it
You forgot a grin emoji. “A bad workman blames his tools”.
There is some decent evidence that I solved it in any case you’re welcome.