# NullReferenceException (853538)

**URL:** <https://discussions.unity.com/t/nullreferenceexception-853538/853538>\
**Category:** Scripting\
**Created:** [August 27, 2021, 2:22pm UTC](https://discussions.unity.com/t/nullreferenceexception-853538/853538 "2021-08-27T14:22:18Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Deathgun12345](https://avatars.discourse-cdn.com/v4/letter/d/d78d45/32.png) [@Deathgun12345](https://discussions.unity.com/u/Deathgun12345)\
**Post date:** [August 27, 2021, 2:22pm UTC](https://discussions.unity.com/t/nullreferenceexception-853538/853538/1 "2021-08-27T14:22:18Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Kurt-Dekker](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/kurt-dekker/32/101578_2.png) [@Kurt-Dekker](https://discussions.unity.com/u/Kurt-Dekker)\
**Post date:** [August 27, 2021, 3:07pm UTC](https://discussions.unity.com/t/nullreferenceexception-853538/853538/2 "2021-08-27T15:07:27Z")

</div>

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

> **[How to find a nullref in Unity3D scripting – PLBM Games](http://plbm.com/?p=221)**

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:

> [@Why do my Music ignore the Sliders ?](https://discussions.unity.com/t/814091/4):
>
> Let me take you step by step through it. You see the error line, line 35? The only possible thing that could be null in that line is audio\_BGMusic So that’s part 1. That’s what’s null. We identified it. Part 2… WHY is it null? Well, first we start with “who is supposed to make it not null?” Looking up further I see line 19 sets it, with this construct: audio\_BGMusic = ObjectMusic.GetComponent\<AudioSource\>(); So lets take that apart. How can that fail? We have to suppose it DID fail, so we…

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

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

> [@NullReferenceException: Object reference not set to an instance of an object.](https://discussions.unity.com/t/840647/7):
>
> Is your particle system assigned to the field in the Inspector on your script? A Null Reference Exception (NRE) is caused when you’re trying to access a method or property on a null object – in other words, trying to get something from nothing. Pretend I give you a set of instructions: I will hand you a package of Oreos. Open the package Take out a cookie. Eat the cookie. Close the package. I hand you the package, you open it, and the container is empty. Your “code” would fail at line 4, be…
