NullReferenceException

xin chào, tôi muốn lập trình một hệ thống hạt hoạt động để khi tôi nhấp vào bất kỳ đối tượng trò chơi nào, nó sẽ hiển thị tại thời điểm đó tôi gặp lỗi này:
NullReferenceException: Tham chiếu đối tượng không được đặt thành một thể hiện của đối tượng
playerhooting.Rpcdohiteffect (Vector3 _pos, Vector3 _normal) (tại Assets / script / playerhooting.cs: 83)
playerhooting.InvokeRpcRpcdohiteffect (UnityEngine.Networking.NetworkBehaviour obj, UnityEngine.Networking.NetworkReader reader)
UnityEngine.Networking.NetworkIdentity.HandleRPC (Int32 cmdHash, UnityEngine.Networking.NetworkReader reader) (tại C: /buildslave/unity/build/Extensions/Networking/Runtime/NetworkIdentity.cs: 660)
UnityEngine.Networking.ClientScene.OnRPCMessage (UnityEngine.Networking.NetworkMessage netMsg) (tại C: /buildslave/unity/build/Extensions/Networking/Runtime/ClientScene.cs: 739)
UnityEngine.Networking.NetworkConnection.InvokeHandler (UnityEngine.Networking.NetworkMessage netMsg) (tại C: /buildslave/unity/build/Extensions/Networking/Runtime/NetworkConnection.cs: 231)
UnityEngine.Networking.LocalClient.ProcessInternalMessages () (tại C: /buildslave/unity/build/Extensions/Networking/Runtime/LocalClient.cs: 141)
UnityEngine.Networking.LocalClient.Update () (tại C: /buildslave/unity/build/Extensions/Networking/Runtime/LocalClient.cs: 69)
UnityEngine.Networking.NetworkClient.UpdateClients () (tại C: /buildslave/unity/build/Extensions/Networking/Runtime/NetworkClient.cs: 965)
UnityEngine.Networking.NetworkIdentity.UNetStaticUpdate () (tại C: /buildslave/unity/build/Extensions/Networking/Runtime/NetworkIdentity.cs: 1091)
đây là phần lệnh:
sử dụng UnityEngine;
sử dụng UnityEngine.Networking;

[RequestComponent (typeof (gamemanager))]
chụp người chơi lớp công khai: NetworkBehaviour
{
private const string PLAYER_TAG = “Trình phát”;
[SerializeField]
Camera cam riêng;

[SerializeField]
mặt nạ LayerMask riêng tư;
trình phát riêngWeapon currentweapon;
vũ khí tư nhân vũ khí quản lý vũ khí;
công khai AudioSource fireound;
voidStart ()
{
if (cam == null)
{
Debug.LogError (“Chụp người chơi: Không có camera nào được tham chiếu”);
this.enabled = false;
}
vũ khí = GetComponent ();
}

void Update ()
{
currentweapon = vũ khí.getcurrentweapon ();
if (currentweapon.firerate <= 0)
{
if (Input.GetButtonDown (“Fire1”))
{
Cháy();

firesound.Play ();

bắn();
}
}
khác
{
if (Input.GetButtonDown (“Fire1”))
{

InvokeRepeating (“Bắn”, 0f, 1 / currentweapon.firerate);
}
else if (Input.GetButtonUp (“Fire1”))
{
CancelInvoke (“Bắn”);
}
}
}

lửa vô hiệu ()
{

}

[Chỉ huy]
void Cmdonhit (Vector3 _pos, Vector3 _normal)
{
Rpcdohiteffect (_pos, _normal);
}

[ClientRpc]
void Rpcdohiteffect (Vector3 _pos, Vector3 _normal)
{
GameObject _hiteffects = Instantiate (vũ khí.getcurrentgraphics (). Hiteffectprefabs, _pos, Quaternion.LookRotation (_normal));
Hủy bỏ (_hiteffects, 2f);
}

[Khách hàng]
void bắn ()
{

if (! isLocalPlayer)
{
trở lại;
}

RaycastHit _hit;
if (Physics.Raycast (cam.transform.position, cam.transform.osystem, out _hit, currentweapon.range, mask))
{
if (_hit.collider.tag == PLAYER_TAG)
{

Cmdplayershot (_hit.collider.name, currentweapon.damge);
}
Cmdonhit (_hit.point, _hit.normal);
}
}
[Chỉ huy]
void Cmdplayershot (string _playerID, int _damge)
{
Debug.Log (_playerID + “đã được bắn.”);

player _players = gamemanager.getplayer (_playerID);
_players.Rpctakedamge (_damge);
}
}

lỗi nằm trong dòng: GameObject _hiteffects = Instantiate (vũ khí.getcurrentgraphics (). hiteffectprefabs, _pos, Quaternion.LookRotation (_normal));
nó làm cho tôi không thể bật hạt
ai đó làm ơn giúp tôi với.

The answer is always the same… ALWAYS. It is the single most common error ever.

Don’t waste your life spinning around and round on this error. Instead, learn how to fix it fast… it’s EASY!!

Some notes on how to fix a NullReferenceException error in Unity3D

  • also known as: Unassigned Reference Exception
  • also known as: Missing Reference Exception
  • also known as: Object reference not set to an instance of an object

The basic steps outlined above are:

  • Identify what is null
  • Identify why it is null
  • Fix that.

Expect to see this error a LOT. It’s easily the most common thing to do when working. Learn how to fix it rapidly. It’s easy. See the above link for more tips.

You need to figure out HOW that variable is supposed to get its initial value. There are many ways in Unity. In order of likelihood, it might be ONE of the following:

  • drag it in using the inspector
  • code inside this script initializes it
  • some OTHER external code initializes it
  • ? something else?

This is the kind of mindset and thinking process you need to bring to this problem:

Step by step, break it down, find the problem.

Here is a clean analogy of the actual underlying problem of a null reference exception: