- Home /
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.
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!
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.
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;
[ExecuteInEdit$$anonymous$$ode]
public class ProgressTracker : $$anonymous$$onoBehaviour
{
//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)$$anonymous$$ath.Round((NewReferent$$anonymous$$anager.Get.CompletedTasks / _TotalTaskCount) * 100);
GUI.Box(GUIRectWindow, "Progress: " + percetange.ToString() + "%");
m_texture = new Texture2D((int)percetange, 20);
GUI.DrawTexture(GUIRectProgressBar, m_texture);
}
}
}
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
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613155455im_/https://answers.unity.com/themes/thub/images/avi.jpg)