在Unity中,在运行时取消分配对象引用。

huangapple go评论58阅读模式
英文:

Object reference unassigning itself at runtime in UNITY

问题

I'm trying to reference the grid object which contains a script that has my inventorygrid class. I'm referencing from another script attached to the ui camera.

在Unity中,在运行时取消分配对象引用。

Its assigned like this:

在Unity中,在运行时取消分配对象引用。

I'm just trying to reference it like this:

public class InventoryController : MonoBehaviour
{
    [SerializeField] private InventoryGrid selectedInventoryGrid;
 
    private void Update()
    {
        if (selectedInventoryGrid == null)
        {
            Debug.Log("InventoryGrid IS null.");
            return;
        }
 
        selectedInventoryGrid.GetTileGridPosition(Input.mousePosition);
    }
}

However, when trying to run the referenced class and method, I get "Object reference not set to an instance of an object.". I've noticed that the "Selected Inventory Grid" that I've assigned just unassigns itself when I run the game.

英文:

I'm trying to reference the grid object which contains a script that has my inventorygrid class. I'm referencing from another script attached to the ui camera.

在Unity中,在运行时取消分配对象引用。

Its assigned like this:

在Unity中,在运行时取消分配对象引用。

I'm just trying to reference it like this:

    public class InventoryController : MonoBehaviour
{
    [SerializeField] private InventoryGrid selectedInventoryGrid;
 
    private void Update()
    {
        if (selectedInventoryGrid = null)
        {
            Debug.Log("InventoryGrid IS null.");
            return;
        }
 
 
        selectedInventoryGrid.GetTileGridPosition(Input.mousePosition);
    }
}

However, when trying to run the referenced class and method, I get "Object reference not set to an instance of an object.". I've noticed that the "Selected Inventory Grid" that I've assigned just unassigns itself when I run the game.

答案1

得分: 3

selectedInventoryGrid = null

它应该是

selectedInventoryGrid == null

我设置为 null 而没有意识到。

英文:
selectedInventoryGrid = null

It should be

selectedInventoryGrid == null

I was setting it null without realising.

答案2

得分: -1

You have two issues. First is already mentioned by @DeveloperLewis.

if (selectedInventoryGrid == null)
{
    Debug.Log("InventoryGrid IS null.");
    return;
}

If you are checking for null, it should be "==" not "=".

Second issue is this field:

[SerializeField] private InventoryGrid selectedInventoryGrid;

I think that some versions of Unity have a bug (or it worked all the time like this?) that they drop the reference if your field is PRIVATE but has [SerializeField] attribute assigned to it.

Solution is that, you need to make that field PUBLIC.

public InventoryGrid selectedInventoryGrid;
英文:

You have two issues. First is already mentioned by @DeveloperLewis.

 if (selectedInventoryGrid = null)
    {
        Debug.Log("InventoryGrid IS null.");
        return;
    }

If you are checking for null, it should be "==" not "=".

Second issue is this field:

    [SerializeField] private InventoryGrid selectedInventoryGrid;

I think that some versions of Unity have a bug (or it worked all the time like this?) that they drop the reference if your field is PRIVATE but has [SerializeField] attribute assigned to it.

Solution is that, you need to make that field PUBLIC.

public InventoryGrid selectedInventoryGrid;

huangapple
  • 本文由 发表于 2023年5月7日 13:23:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76192313.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定