-1

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)

enter image description here

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

Hierachy

And here is my Inspector

Inspector

I hope you guys understood problem :-)

  • That's a very common exception. It means that something is `null` and you're trying to access its members or methods. You can't get any data from `null`. In this specific case, probably `transform.Find` can't find any GameObject. – Mateo Jul 11 '22 at 02:06

1 Answers1

0

I think you add Componenet "ShootingBullet.cs" to other object. transform.find(~) return child transform or null if no child is found. you may use GameObject.FindObjectOfType<Recoil>(); not transform.Find("CameraRot/CameraRecoil").GetComponent<Recoil>(); or add "ShootingBullet.cs" to right Object.(ex. CameraRot and use transform.Find("CameraRecoil").GetComponent<Recoil>(); if It worked well, please let me know thanks.

blackTree
  • 97
  • 6
  • `FindObjectOfType` assumes there will always be only one in the scene .. in that case that might be true ... the more general solution might be `GetComponentInChildren` in order to only search in the hierarchy under a certain parent object ... even better though would be to simply already reference it in a field via the Inspector – derHugo Jul 11 '22 at 07:17