Unity 4.1.5 is not showing certain public variables in the inspector
Before you point out that Unity 4.1.5 is ancient, I know. I'm using it to follow a specific tutorial that I will update to 5.~.~ later.
So I have this little problem, where certain public values don't show up in the inspector.
using UnityEngine;
using System.Collections.Generic;
using RTS;
public class HUD : MonoBehaviour
{
public GUISkin resourceSkin, ordersSkin, selectBoxSkin, mouseCursorSkin;
public Texture2D activeCursor;
public Texture2D selectCursor, leftCursor, rightCursor, upCursor, downCursor;
public Texture2D[] attackCursors, harvestCursors, moveCursors;
public Texture2D[] resources;
private Player player;
private CursorState activeCursorState;
private int currentFrame = 0;
private Dictionary<ResourceType, int> resourceValues, resourceLimits;
private Dictionary<ResourceType, Texture2D> resourceImages;
private const int ORDERS_BAR_WIDTH = 150, RESOURCE_BAR_HEIGHT = 40;
private const int SELECTION_NAME_HEIGHT = 15;
private const int ICON_WIDTH = 32, ICON_HEIGHT = 32, TEXT_WIDTH = 128, TEXT_HEIGHT = 32;
/*** Game Engine Methods ***/
void Start()
{
player = transform.root.GetComponent<Player>();
resourceValues = new Dictionary<ResourceType, int>();
resourceLimits = new Dictionary<ResourceType, int>();
resourceImages = new Dictionary<ResourceType, Texture2D>();
for (int i = 0; i < resources.Length; i++)
{
switch (resources[i].name)
{
case "Money":
resourceImages.Add(ResourceType.Money, resources[i]);
resourceValues.Add(ResourceType.Money, 0);
resourceLimits.Add(ResourceType.Money, 0);
break;
case "Power":
resourceImages.Add(ResourceType.Power, resources[i]);
resourceValues.Add(ResourceType.Power, 0);
resourceLimits.Add(ResourceType.Power, 0);
break;
default: break;
}
}
ResourceManager.StoreSelectBoxItems(selectBoxSkin);
SetCursorState(CursorState.Select);
}
void OnGUI()
{
//we only want to draw a GUI for human players
if (player.human)
{
DrawOrdersBar();
DrawResourceBar();
//call last to ensure that the custom mouse cursor is seen on top of everything
DrawMouseCursor();
}
}
/*** Public methods for interacting with the HUD ***/
public bool MouseInBounds()
{
//Screen coordinates start in the lower-left corner of the screen
//not the top-right of the screen like the drawing coordinates do
Vector3 mousePos = Input.mousePosition;
bool insideWidth = mousePos.x >= 0 && mousePos.x <= Screen.width - ORDERS_BAR_WIDTH;
bool insideHeight = mousePos.y >= 0 && mousePos.y <= Screen.height - RESOURCE_BAR_HEIGHT;
return insideWidth && insideHeight;
}
public Rect GetPlayingArea()
{
return new Rect(0, RESOURCE_BAR_HEIGHT, Screen.width - ORDERS_BAR_WIDTH, Screen.height - RESOURCE_BAR_HEIGHT);
}
public void SetCursorState(CursorState newState)
{
activeCursorState = newState;
switch (newState)
{
case CursorState.Select:
activeCursor = selectCursor;
break;
case CursorState.Attack:
currentFrame = (int)Time.time % attackCursors.Length;
activeCursor = attackCursors[currentFrame];
break;
case CursorState.Harvest:
currentFrame = (int)Time.time % harvestCursors.Length;
activeCursor = harvestCursors[currentFrame];
break;
case CursorState.Move:
currentFrame = (int)Time.time % moveCursors.Length;
activeCursor = moveCursors[currentFrame];
break;
case CursorState.PanLeft:
activeCursor = leftCursor;
break;
case CursorState.PanRight:
activeCursor = rightCursor;
break;
case CursorState.PanUp:
activeCursor = upCursor;
break;
case CursorState.PanDown:
activeCursor = downCursor;
break;
default: break;
}
}
public void SetResourceValues(Dictionary<ResourceType, int> resourceValues, Dictionary<ResourceType, int> resourceLimits)
{
this.resourceValues = resourceValues;
this.resourceLimits = resourceLimits;
}
/*** Private Worker Methods ***/
private void DrawOrdersBar()
{
GUI.skin = ordersSkin;
GUI.BeginGroup(new Rect(Screen.width - ORDERS_BAR_WIDTH, RESOURCE_BAR_HEIGHT, ORDERS_BAR_WIDTH, Screen.height - RESOURCE_BAR_HEIGHT));
GUI.Box(new Rect(0, 0, ORDERS_BAR_WIDTH, Screen.height - RESOURCE_BAR_HEIGHT), "");
string selectionName = "";
if (player.SelectedObject)
{
selectionName = player.SelectedObject.objectName;
}
if (!selectionName.Equals(""))
{
GUI.Label(new Rect(0, 10, ORDERS_BAR_WIDTH, SELECTION_NAME_HEIGHT), selectionName);
}
GUI.EndGroup();
}
private void DrawResourceBar()
{
GUI.skin = resourceSkin;
GUI.BeginGroup(new Rect(0, 0, Screen.width, RESOURCE_BAR_HEIGHT));
GUI.Box(new Rect(0, 0, Screen.width, RESOURCE_BAR_HEIGHT), "");
int topPos = 4, iconLeft = 4, textLeft = 20;
DrawResourceIcon(ResourceType.Money, iconLeft, textLeft, topPos);
iconLeft += TEXT_WIDTH;
textLeft += TEXT_WIDTH;
DrawResourceIcon(ResourceType.Power, iconLeft, textLeft, topPos);
GUI.EndGroup();
}
private void DrawResourceIcon(ResourceType type, int iconLeft, int textLeft, int topPos)
{
Texture2D icon = resourceImages[type];
string text = resourceValues[type].ToString() + "/" + resourceLimits[type].ToString();
GUI.DrawTexture(new Rect(iconLeft, topPos, ICON_WIDTH, ICON_HEIGHT), icon);
GUI.Label(new Rect(textLeft, topPos, TEXT_WIDTH, TEXT_HEIGHT), text);
}
private void DrawMouseCursor()
{
bool mouseOverHud = !MouseInBounds() && activeCursorState != CursorState.PanRight && activeCursorState != CursorState.PanUp;
if (mouseOverHud)
{
Screen.showCursor = true;
}
else
{
Screen.showCursor = false;
GUI.skin = mouseCursorSkin;
GUI.BeginGroup(new Rect(0, 0, Screen.width, Screen.height));
UpdateCursorAnimation();
Rect cursorPosition = GetCursorDrawPosition();
GUI.Label(cursorPosition, activeCursor);
GUI.EndGroup();
}
}
private void UpdateCursorAnimation()
{
//sequence animation for cursor (based on more than one image for the cursor)
//change once per second, loops through array of images
if (activeCursorState == CursorState.Move)
{
currentFrame = (int)Time.time % moveCursors.Length;
activeCursor = moveCursors[currentFrame];
}
else if (activeCursorState == CursorState.Attack)
{
currentFrame = (int)Time.time % attackCursors.Length;
activeCursor = attackCursors[currentFrame];
}
else if (activeCursorState == CursorState.Harvest)
{
currentFrame = (int)Time.time % harvestCursors.Length;
activeCursor = harvestCursors[currentFrame];
}
}
private Rect GetCursorDrawPosition()
{
//set base position for custom cursor image
float leftPos = Input.mousePosition.x;
float topPos = Screen.height - Input.mousePosition.y; //screen draw coordinates are inverted
//adjust position base on the type of cursor being shown
if (activeCursorState == CursorState.PanRight) leftPos = Screen.width - activeCursor.width;
else if (activeCursorState == CursorState.PanDown) topPos = Screen.height - activeCursor.height;
else if (activeCursorState == CursorState.Move || activeCursorState == CursorState.Select || activeCursorState == CursorState.Harvest)
{
topPos -= activeCursor.height / 2;
leftPos -= activeCursor.width / 2;
}
return new Rect(leftPos, topPos, activeCursor.width, activeCursor.height);
}
}
Specifically, these two lines:
public Texture2D[] attackCursors, harvestCursors, moveCursors;
public Texture2D[] resources;
They should be Texture2D's, but aren't.
Answer by tanoshimi · Nov 20, 2016 at 01:47 PM
They should be arrays of Texture2Ds, which they are. But since you haven't assigned any elements to that array (i.e. the Size is 0 for every variable), there's nothing to show. Increase the size of the array, of just drag and drop a texture2D onto the array and Unity will dynamically resize it for you.
How do I do this? I'm still knew to program$$anonymous$$g, and I'm not yet proficient. I was able to get some of the proper icons to show up on the HUD, but they aren't sized properly.