• 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 /
avatar image
Question by mmangual_83 · Jul 31, 2014 at 02:41 PM · c#gui

Issues creating a progress bar

I am tasked with creating a progress bar using Unity's GUI system. The way I am creating this GUI is that I made a background window and then I added a Texture2D that will represent the progress bar.

alt text

I got this showing on the screen during run time, which is good, but it spams an error message in my console window; it reads: Exception: not implemented and it points to a line of code found here: (I will go ahead and add the rest of my code)

 using UnityEngine;
 using System.Collections;
 using Simulation;
 using System;
 [ExecuteInEditMode]
 public class ProgressTracker : MonoBehaviour
 {
     //Singleton
     public static ProgressTracker objectInstance;
 
     //GUI Elements
     public Rect GUIRectWindow; // the GUI background for the progress tracker
     public Rect GUIRectProgressBar; // the GUI background for the progress tracker
     public Texture2D m_texture; // draws our progress bar (I have a small white square)
     private int _TotalGestureCount = 0;
     private float percetange;
 
     void Start()
     {
         if (objectInstance == null)
         {
             objectInstance = this;//keep using the same game object
             DontDestroyOnLoad(gameObject);
         }
         else
             Destroy(this);
 
         percetange = 0;
         _TotalTaskCount = TaskCollection._tasksSum;        
     }
 
     // Update is called once per frame
     void OnGUI()
     {
         if (!Application.isLoadingLevel)
         {
             percetange = (NewReferentManager.Get.CompletedTasks / _TotalTaskCount) * 100; //calculates the current percentage
             GUI.Box(GUIRectWindow, "Progress: " + percetange.ToString() + "%");// outputs the percentage
             GUI.DrawTexture(GUIRectProgressBar, m_texture);//draws our backdrop window
             
             m_texture.width = (int)percetange; // THIS IS WHERE I AM GETTING THE ERROR
         }
     }
 }


Can anyone help me figure out why I am getting this error? Thank you in advance!

progressbar issues.png (3.8 kB)
Comment

People who like this

0 Show 3
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 LMan · Jul 31, 2014 at 02:47 PM 0
Share

What happens if you don't cast to an Int there?

avatar image mmangual_83 · Jul 31, 2014 at 02:48 PM 0
Share

it tells me that I cannot convert from int to float. Basically from what I gather, its anytime I try and modify the texture2d's width or height.

avatar image mmangual_83 · Jul 31, 2014 at 03:20 PM 0
Share

Okay, so I updated the code and solved my little exception issue. Apparently, the width and height properties are read-only so doing something like m_texture,width = 10; would be wrong. I looked around the web and came across someone who was having the same issue I am having

and thankfully managed to solve it by making a new instance of the texture within my Update() function. I tested it out and indeed the progress bar grows, but... unfortunately it only sticks to 1% all the time. Can anyone help me find out whats wrong with my code now? Here is the updated code:

 using UnityEngine;
 using System.Collections;
 using Simulation;
 using System;
 [ExecuteInEditMode]
 public class ProgressTracker : MonoBehaviour
 {
     //Singleton
     public static ProgressTracker objectInstance;
 
     //GUI Elements
     public Rect GUIRectWindow; // the GUI background for the progress tracker
     public Rect GUIRectProgressBar; // the GUI background for the progress tracker
     public Texture2D m_texture;
     private int _TotalTaskCount = 0;
     private float percetange;
     
     void Start()
     {
         if (objectInstance == null)
         {
             objectInstance = this;//keep using the same game object
             DontDestroyOnLoad(gameObject);
         }
         else
             Destroy(this);
 
         percetange = 0;
         _TotalTaskCount = TaskCollection._tasksSum;   
         
     }
 
     // Update is called once per frame
     void OnGUI()
     {
         if (!Application.isLoadingLevel)
         {
             percetange = (float)Math.Round((NewReferentManager.Get.CompletedTasks / _TotalTaskCount) * 100);
             GUI.Box(GUIRectWindow, "Progress: " + percetange.ToString() + "%");
             m_texture = new Texture2D((int)percetange, 20);
             GUI.DrawTexture(GUIRectProgressBar, m_texture);            
         }
     }
 }

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by mmangual_83 · Jul 31, 2014 at 05:10 PM

I figured out what the problem was. Here is the complete code to help anyone else who may be having the same issue:

 using UnityEngine;
 using System.Collections;
 using Simulation;
 using System;
 [ExecuteInEditMode]
 public class ProgressTracker : MonoBehaviour
 {
     //Singleton
     public static ProgressTracker objectInstance;
 
     //GUI Elements
     public Rect GUIRectWindow; // the GUI background for the progress tracker
     public Texture2D m_texture;
     private int _TotalTaskCount = 0;
     private float percetange;
     private const int _MAXVAL = 150;
     private Vector2 progressBarPos = new Vector2(26, 53);
     private Vector2 progressBarSize = new Vector2(_MAXVAL, 20);
     void Start()
     {
         if (objectInstance == null)
         {
             objectInstance = this;//keep using the same game object
             DontDestroyOnLoad(gameObject);
         }
         else
             Destroy(this);
 
         percetange = 0;
         _TotalTaskCount = TaskCollection._tasksSum;          
     }
 
     // Update is called once per frame
     void OnGUI()
     {
         if (!Application.isLoadingLevel)
         {
             percetange = (float)Math.Round((NewReferentManager.Get.CompletedTasks / _TotalTaskCount) * 100);
             GUI.Box(GUIRectWindow, "Progress: " + percetange.ToString() + "%");
 
             GUI.DrawTexture(new Rect(progressBarPos.x, progressBarPos.y, //position
                                      percetange * Mathf.Clamp01(progressBarSize.x), progressBarSize.y), //size
                                      m_texture); //the texture            
         }
     }
 }

Comment

People who like this

0 Show 0 · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

C# Multiple GUI.Tooltips 2 Answers

Getting Debug log errors when I load my scenes 2 Answers

My text field is not taking any input 1 Answer

Cannot modify a value type return value of... 1 Answer

Creating 2D art in a 3D world 3 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