NullReferenceException: referencia de objeto no establecida en una instancia de un objeto (Unity C#) [duplicate]
– UnityAssets3Free
bienvenido , por aqui Daniel y en esta ocasion os traigo
esta pregunta
Esta pregunta ya tiene respuestas aquí: ¿Qué es una NullReferenceException y cómo la soluciono? (27 respuestas) Cerrado el mes pasado.
Soy nuevo en Unity Game Dev. Estoy haciendo un juego multiplicador de fps, así que agregué retroceso al ver este video https://www.youtube.com/watch?v=geieixA4Mqc
hice lo mismo que hizo vid y dio el error
NullReferenceException: Object reference not set to an instance of an object
ShootingBullet.Start () (at Assets/Script/ShootingBullet.cs:28)
este es mi codigo
retroceso.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();
Lo siento, no di todo mi código, pero el código principal está aquí,
disparar bala() se llama Cuando haga clic izquierdo.
y aquí está el mío Jerarquía
y aquí está el mío Inspector
Espero que entiendas el problema 🙂
1 respuesta 1
Creo que agrega Componenet «ShootingBullet.cs» a otro objeto. transform.find(~) devuelve la transformación secundaria o nula si no se encuentran elementos secundarios. usted puede utilizar
GameObject.FindObjectOfType<Recoil>();
no
transform.Find("CameraRot/CameraRecoil").GetComponent<Recoil>();
o agregue «ShootingBullet.cs» al objeto correcto. (por ejemplo, CameraRot y uso transform.Find("CameraRecoil").GetComponent<Recoil>();
si funcionó bien por favor hágamelo saber gracias.
nota: si aun no se resuelve tu pregunta por favor dejar un comentario y pronto lo podremos de nuevo , muchas gracias
sin mas,espero que te halla servido