Register Now

Login

Lost Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Login

Register Now

(Solution) Unity 3D NullReferenceException: Object reference not set Error

Semptom

You will encounter the following error. And the action you command will not take place.

NullReferenceException: Object reference not set to an instance of an object
sureScript.Update () (at Assets/sureScript.cs:41)

Reason

The reason you see this error is that the definitions or the data in it that you tried to make operations on your variables or arrays could not be found.

If you click on the error in the console, the object with the problem script is shown in yellow on the hierarchy panel.

Example 1 (Detailed)

In the example below, two arrays are defined, namely arrayName1 and arrayName2.
Arrays are created in a usable way by determining the number of array elements in the start function.
The contents of the array elements defined in the Update section have been loaded.
And finally, with the print command, the third element of both directories is ordered to be written to the console.

 

public int[] diziAdi1,diziAdi2;

public class OrnekScript
{

Start
{
diziAdi1 = new int[3];
diziAdi1 = new int[3];
}

Update
{

diziAdi1[0] = “571”;
diziAdi1[1] = “612”;
diziAdi1[2] = “1024”;
diziAdi2[0] = “572”;
diziAdi2[1] = “613”;
diziAdi2[2] = “1025”;

print(“DiziAdi1 3.elemanı” + diziAdi1[3]);
print(“DiziAdi2 3.elemanı” + diziAdi2[3]);
}

}

Example 2 (Simple)

As can be seen in this example, a Text is created on the canvas and it is aimed to transfer text to this text. There is no problem in the code part. But there is something overlooked, let’s examine the solution …

using UnityEngine.UI;

public Text yazilik;

public class OrnekScript
{
start()
{
yazilik.Text = “Deneme Bir İki Üç”;
}

}

Error Solution

Example 1 Solution

According to this example, a detailed error will appear in the console as follows.

OrnekScript.Update () (at Assets/sureScript.cs:(satırsayısı))

If you examine the sample carefully, you will notice the problem immediately. In the definition part, arrays are named as arrayName1 and arrayName2. However, when determining the number of array elements in the Start function, both the array names were the same (arrayName1). Absence can cause such errors. And on top of that, it has been tried to provide data transfer to the elements of arrayName2 in the Update function. Of course, since the array definition is not correct, the system will give an error stating that the definitions are not found. As you would appreciate, all that needs to be done here is to change the second repeated arrayName1 to arrayName2. This problem will be solved.

Example 2 Solution

If you have created a Text on your Canvas and want to transfer text to this Text with script. It will ask you to place the Text object named “text” in the Inspector part of the object your script is in. If you do not place it here, you will see the text None (Text).

 

Articles that may be of interest;

Unity 3D C# derleme hataları nasıl yorumlanır ?

Bağımsız(indie) Oyun Geliştiricisi kime denir ? ve Neler yapar?

Leave a reply