I'm afraid I don't understand what you mean by "Is the menu involved on at the time?".
The RawImage referenced by the code is intended to come from one of your Menus. I'm asking if this menu is, or has been, made visible at the time the error shows.
I tried what you suggested above, but it keeps returning a Null Reference again.
Try this:
using UnityEngine;
using UnityEngine.UI;
using AC;
public class HoverItemProperties : MonoBehaviour
{
private RawImage largeImage;
public string menuName;
public int largeImageConstantID;
private Menu menu;
private InvItem hoverItem;
private void Update ()
{
if (KickStarter.runtimeInventory.hoverItem != hoverItem)
{
SetHoverItem (KickStarter.runtimeInventory.hoverItem);
}
}
private void SetHoverItem (InvItem item)
{
hoverItem = item;
menu = PlayerMenus.GetMenuWithName (menuName);
if (menu.RuntimeCanvas)
{
largeImage = (RawImage) Serializer.GetGameObjectComponent <RawImage> (4896362, menu.RuntimeCanvas.gameObject);
}
if (hoverItem == null)
{
menu.TurnOff ();
}
else
{
largeImage.texture = item.tex;
menu.TurnOn ();
}
}
}
Well the menu is supposed to appear when I hover over an inventory item, but as soon as I get the cursor over the item, the game crashes and gives me the error i.e. the menu doesn't get to appear. The menu itself appears normally on hover without the RawImage lines in script.
Tried the latest script you posted - same error, just on a different line ("largeImage.texture = item.tex;"):
NullReferenceException: Object reference not set to an instance of an object
HoverItemProperties.SetHoverItem (AC.InvItem item) (at Assets/GUI/TooltipUnity/HoverItemProperties.cs:42)
HoverItemProperties.Update () (at Assets/GUI/TooltipUnity/HoverItemProperties.cs:21)
So I reread the whole topic to find if I missed something, and apparently I did - it seems I've been using some "wrong" lines in the script when it comes to what I'm actually trying to accomplish. I'm trying to load "rarity" .pngs from the Resources folder (Image 1) to appear via the "Image" component of the menu when hovering over inventory items.
I've defined the Inventory property "Filename" as a String type (Image 2), and then typed in the name of one file from the Resources folder (in this case "common") in the "Filename property" field of an inventory item in the Inspector's tab (Image 3).
And then I put together the code at the bottom to the best of my knowledge, but I'm definitely missing something because I keep getting an error again when hovering over Inventory items:
NullReferenceException: Object reference not set to an instance of an object
HoverItemProperties.SetHoverItem (AC.InvItem item) (at Assets/GUI/TooltipUnity/HoverItemProperties.cs:43)
HoverItemProperties.Update () (at Assets/GUI/TooltipUnity/HoverItemProperties.cs:21)
i.e. on line "largeImage.texture = Resources.Load (filename) as Texture2D;" of this new script I put together.
The complete script:
using UnityEngine;
using UnityEngine.UI;
using AC;
public class HoverItemProperties : MonoBehaviour
{
private RawImage largeImage;
public string menuName;
public int largeImageConstantID;
private Menu menu;
private InvItem hoverItem;
private void Update ()
{
if (KickStarter.runtimeInventory.hoverItem != hoverItem)
{
SetHoverItem (KickStarter.runtimeInventory.hoverItem);
}
}
private void SetHoverItem (InvItem item)
{
hoverItem = item;
menu = PlayerMenus.GetMenuWithName (menuName);
if (menu.RuntimeCanvas)
{
largeImage = (RawImage) Serializer.GetGameObjectComponent <RawImage> (4896362, menu.RuntimeCanvas.gameObject);
}
if (hoverItem == null)
{
menu.TurnOff ();
}
else
{
string filename = KickStarter.runtimeInventory.hoverItem.GetProperty (2).TextValue;
largeImage.texture = Resources.Load (filename) as Texture2D;
menu.TurnOn ();
}
}
}
I apologize for not providing more information on this from the get-go. I think this narrows down the possibilities of what I might be doing wrong here. As always, your help is much appreciated Chris.
AC can now link Graphic menu elements to RawImage components. If you create a new Graphic element in the Menu, you can map it to the "Image" object in your UI prefab. You can then control the graphic's texture as part of the script:
using UnityEngine;
using UnityEngine.UI;
using AC;
public class HoverItemProperties : MonoBehaviour
{
public string menuName;
public string graphicName = "MyGraphic";
private InvItem hoverItem;
private void Update ()
{
if (KickStarter.runtimeInventory.hoverItem != hoverItem)
{
SetHoverItem (KickStarter.runtimeInventory.hoverItem);
}
}
private void SetHoverItem (InvItem item)
{
hoverItem = item;
Menu menu = PlayerMenus.GetMenuWithName (menuName);
if (hoverItem == null)
{
menu.TurnOff ();
}
else
{
menu.TurnOn ();
MenuGraphic graphic = (MenuGraphic) menu.GetElementWithName (graphicName);
string filename = KickStarter.runtimeInventory.hoverItem.GetProperty (2).TextValue;
graphic.SetNormalGraphicTexture (Resources.Load (filename) as Texture2D);
}
}
}
Comments
The RawImage referenced by the code is intended to come from one of your Menus. I'm asking if this menu is, or has been, made visible at the time the error shows.
Try this:
Well the menu is supposed to appear when I hover over an inventory item, but as soon as I get the cursor over the item, the game crashes and gives me the error i.e. the menu doesn't get to appear. The menu itself appears normally on hover without the RawImage lines in script.
Tried the latest script you posted - same error, just on a different line ("largeImage.texture = item.tex;"):
NullReferenceException: Object reference not set to an instance of an object
HoverItemProperties.SetHoverItem (AC.InvItem item) (at Assets/GUI/TooltipUnity/HoverItemProperties.cs:42)
HoverItemProperties.Update () (at Assets/GUI/TooltipUnity/HoverItemProperties.cs:21)
So I reread the whole topic to find if I missed something, and apparently I did - it seems I've been using some "wrong" lines in the script when it comes to what I'm actually trying to accomplish. I'm trying to load "rarity" .pngs from the Resources folder (Image 1) to appear via the "Image" component of the menu when hovering over inventory items.
Image 1: https://ibb.co/2WtyLY8
I've defined the Inventory property "Filename" as a String type (Image 2), and then typed in the name of one file from the Resources folder (in this case "common") in the "Filename property" field of an inventory item in the Inspector's tab (Image 3).
Image 2: https://ibb.co/52qgT1z
Image 3: https://ibb.co/c3SVMXY
And then I put together the code at the bottom to the best of my knowledge, but I'm definitely missing something because I keep getting an error again when hovering over Inventory items:
NullReferenceException: Object reference not set to an instance of an object
HoverItemProperties.SetHoverItem (AC.InvItem item) (at Assets/GUI/TooltipUnity/HoverItemProperties.cs:43)
HoverItemProperties.Update () (at Assets/GUI/TooltipUnity/HoverItemProperties.cs:21)
i.e. on line "largeImage.texture = Resources.Load (filename) as Texture2D;" of this new script I put together.
The complete script:
using UnityEngine;
using UnityEngine.UI;
using AC;
public class HoverItemProperties : MonoBehaviour
{
}
I apologize for not providing more information on this from the get-go. I think this narrows down the possibilities of what I might be doing wrong here. As always, your help is much appreciated Chris.
Thanks for the details.
AC can now link Graphic menu elements to RawImage components. If you create a new Graphic element in the Menu, you can map it to the "Image" object in your UI prefab. You can then control the graphic's texture as part of the script:
That was exactly what I needed, works perfectly now. Again, many thanks for the stellar support!