0

The code above has an array that is filled with imageobjects manually filled via the inspector in unity. But when I try to change the sprite of the image in index 0 of the list I get the error message:

NullReferenceException: Object reference not set to an instance of an object at line 13 which indicates that the array is null, which doesn't make sense.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class Inventory : MonoBehaviour
{
    public Image[] characters;
    public Sprite character;

    public void createInventory(){
                for (int i = 0; i < characters.Length; i++){
                    characters[i].sprite = character;
                    Debug.Log(characters); //object reference not set to an instance of an object
                }
            }        
}
KiynL
  • 4,097
  • 2
  • 16
  • 34
  • Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – madreflection Jun 06 '22 at 15:23
  • Not programming C++ anymore, but the fact that it is a field means that it isn't automatically constructed, right? Otherwise you would require explicit `= NULL` statements. – Maarten Bodewes Jun 06 '22 at 15:29
  • @madreflection i know what the error message means, what i am baffled about, is how the imageobject in the array can be null as i dragged it in the inspector myself, and the array contains 10 imageobject. Can an imageobject be null, and if so, how do i make it "not null"? –  Jun 06 '22 at 15:31
  • From where are you calling createInventory? – Erik Overflow Jun 06 '22 at 15:37
  • @ErikOverflow I am calling createInventory from another script linked to another scene, could this be the problem? –  Jun 06 '22 at 15:41
  • Can you share a screenshot of the inspector with Inventory object's configuration, as well as a few lines of code where you are calling createInventory? That might provide some more context here. – Erik Overflow Jun 06 '22 at 15:42
  • @ErikOverflow ``` public class MainMenu : MonoBehaviour { public Inventory InventoryS; public void Inventory (){ SceneManager.LoadScene(2); InventoryS.createInventory(); } } ``` This is the code where i call the createinventory from another script and scene. I am not sure how to share a screenshot or what you mean by inventory objects configuration, but I checked that the characters array is not empty, and that I have assigned multiple Imageobjects to it. –  Jun 06 '22 at 15:52
  • Ok, well there's your problem. LoadScene actually just queues up a scene change, but doesn't complete until the end of frame. You will be trying to call InventoryS.createInventory on objects in the prior scene, but loading the new one at the same time. – Erik Overflow Jun 06 '22 at 15:56
  • @ErikOverflow Interesting, so how would i go about fixing this, as i do need to change scene and simultaneously load the scene. –  Jun 06 '22 at 15:58
  • You'll need to trigger the "createInventory" from a script within the new scene that is being loaded, rather than the old scene. You're calling "createInventory" on a gameObject that is being destroyed with the scene change, currently. – Erik Overflow Jun 06 '22 at 18:10

1 Answers1

0

The error does not refer to the whole array. It is caused by one of its elements - characters[i].sprite (at line 13). I assume that one of your array elements in the inspector equals to null (like on the screenshot). inspector

AlexEjik
  • 131
  • 4