Im new to Unity Game Dev. Im making a multiplier fps game so i added recoil by watching this video https://www.youtube.com/watch?v=geieixA4Mqc i did same thing as vid did and got the error
NullReferenceException: Object reference not set to an instance of an object
ShootingBullet.Start () (at Assets/Script/ShootingBullet.cs:28)
This is my Code
Recoil.cs
using UnityEngine;
public class Recoil : MonoBehaviour
{
//Rotation
private Vector3 currentRotation;
private Vector3 targetRotation;
//HipFire Recoil
[SerializeField] private float recoilX;
[SerializeField] private float recoilY;
[SerializeField] private float recoilZ;
//Settings
[SerializeField] private float snapiness;
[SerializeField] private float returnSpeed;
void Start()
{
}
void Update()
{
targetRotation = Vector3.Lerp(targetRotation, Vector3.zero, returnSpeed * Time.deltaTime);
currentRotation = Vector3.Slerp(currentRotation, targetRotation, snapiness * Time.fixedDeltaTime);
transform.localRotation = Quaternion.Euler(currentRotation);
}
public void RecoilFire()
{
targetRotation += new Vector3(recoilX, Random.Range(-recoilY, recoilY), Random.Range(-recoilZ, recoilZ));
}
}
ShootingBullet.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ShootingBullet : MonoBehaviour
{
public Recoil Recoil_Script;
void Start()
{
Recoil_Script=transform.Find("CameraRot/CameraRecoil")
.GetComponent<Recoil>();
}
void Update()
{
}
public void ShootBullet()
{
Recoil_Script.RecoilFire();
}
}
Sorry I didnt gave all my code but main things/Code are here, ShootBullet() is called When Left Click.
And here is my Hierachy
And here is my Inspector
I hope you guys understood problem :-)