英文:
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.
Its assigned like this:
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.
Its assigned like this:
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;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论