I’m simply trying to access an array of 2D textures and print “hi” if nothing is in the array slot.
In one script I have:
function Update(){
for(i = 0; i < 49; i++){
if(GetComponent(HUDAndInventory).inventoryTextures *== null){*
-
print("hi");*
-
}*
- }*
}
In the script “HUDAndInventory” there is a total of 50 textures in the “inventoryTextures” array. I just want to iterate through each texture slot and print “hi” if nothing is assigned to it. I get the error: NullReferenceException: Object reference not set to an instance of an object. Now I understand that this error means I’m trying to reference something that isn’t their, but every thing im referencing and accessing is their. So why do I get this error and how can I fix it?
Thanks!
Try this
function Start(){
HUDAndInventory hud = GetComponent<HUDAndInventory>();
}
function Update(){
for(i = 0; i < hud.inventoryTextures.Count; i++){
if(hud.inventoryTextures *== null){*
Debug.Log(“No Texture Found”);
}
}
}
For this to succesfully work you need to have this script and the HUDAndInventory script on the same GameObject. Hope this helped 
system
3
Hi MrSplosion,
If you define your array as a static var outside of your actual function you will be able to access it from other scripts / functions in your project. This will make it globally available to access.
for example:
static var yourArrayName:Array=new Array();
Hope this helps.
Cheers, Chris