Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by CaptClockobob · Nov 20, 2016 at 01:45 PM · c#inspectorpublic variable

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.

alt text

untitled.png (31.2 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image CaptClockobob · Nov 21, 2016 at 04:08 AM 0
Share

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.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

GameObject fields evaluate to null, but they are actually assigned and working 1 Answer

Public fields not showing in inspector 1 Answer

Setting value in inspector does not update script. 0 Answers

Public variables not showing up 0 Answers

Drawing a scene in the inspector GUI 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges