- Home /
Dynamically Adjust Height of Bar Chart (GetComponent error?)
Hi, really new to C# and love it but incredibly frustrated right now at somewhat simple tasks. I want to dynamically change heights of bars in a chart.
I followed this tutorial to learn how to create 2d bar charts using the UI: the title of the video was "Unity Tutorial: Bar Chart"
Now, I've been trying to dynamically alter the heights of those bars and I can't seem to make it happen.
I have a Panel called "Bar Chart" that contains a Prefab called BarHolder that contains two scripts. One of them, called "BarChart" manages the heights of the bars.
My final goal is to be able to allow users to choose how many bars they want, and be able to assign values to each bar. To do this, I need to be able to dynamically change the bar heights. I want these heights to display whatever the user wants. Right now, to test things, I've made a script called "Changer" that is tied to an Empty Object called "EmptyObject4Script".
My code for it is below. All it does is create an int array, size 3, and assigns random values into it. I did this just to check if I could change the heights of the bars from this alone.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Changer : MonoBehaviour {
public int[] myValues = new int[3]; //variable called 'myvalues' of size 3 made up of ints
public int randomVal;
//function that assigns random values to the values thing
void ChangerThing(){
myValues [0] = Random.Range (1, 255);
myValues [1] = Random.Range (1, 255);
myValues [2] = Random.Range (1, 255);
randomVal = Random.Range (1, 255);
}
// Update is called once per frame
void Update () {
ChangerThing(); // launch function
Debug.Log ("myvalues=" + myValues[1].ToString());
Debug.Log (randomVal);
}
}
Next, I wanted to 'import' the variable myValues into the BarChart Script so that it changes the heights...but here is where I am getting stuck! I've marked the parts that didn't work in the comments. I get the error that "an object reference is required to access non-static member" in all attempts.
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI; // cus using UI so need to do unityengine.ui import
using UnityEngine;
using System.Linq; //sorting of arrays a lot of data management
public class BarChart : MonoBehaviour {
public Bar barPrefab; //creates a fillable object thing in the inspector of BarChart to which we add the barprefab we made
public int[] inputValues; //array of input values (means i can
public string[] labels;
public Color[] colors;
List<Bar> bars = new List<Bar>();
float chartHeight;
void Start (){
//*******This BELOW doesn't work
//GameObject EmptyObject4Script = GameObject.Find ("EmptyObject4Script");
//Changer changerScript = Changer.GetComponent<Changer> ();
chartHeight = Screen.height + GetComponent<RectTransform>().sizeDelta.y;
//Display Graph
//float[] values = {0.1f, 0.5f, 0.7f};
DisplayGraph(inputValues);
}
void Update(){
//********THIS BELOW DOESNT WORK
//GameObject EmptyObject4Script = GameObject.Find ("EmptyObject4Script");
//Changer changerScript = Changer.GetComponent<Changer> ();
//********THIS BELOW DOESNT WORK EITHER
Changer changer = GetComponent<Changer> ();
Debug.Log (Changer.myValues); //check to see if values show up
valuesToDisplay = Changer.myValues;
//run function
DisplayGraph (valuesToDisplay);
}
void DisplayGraph(int[] vals){ //float[] vals stands for the input values that we send into function when we activate it
int maxValue = vals.Max();
//creates bars and applies sizing to those bars
for (int i = 0; i < vals.Length; i++) {
Bar newBar = Instantiate (barPrefab) as Bar;
newBar.transform.SetParent (transform); //set parrent to the transform (the rect transform of the empty object)
RectTransform rt = newBar.bar.GetComponent<RectTransform>();
float normalizedValue = (float)vals [i] / (float)maxValue * 0.90f;
rt.sizeDelta = new Vector2 (rt.sizeDelta.x, chartHeight * normalizedValue);
newBar.bar.color = colors [i % colors.Length];
if (labels.Length <= i) {
newBar.label.text = "undefined";
} else {
newBar.label.text = labels [i];
}
newBar.barValue.text = vals [i].ToString(); //label the values
if (rt.sizeDelta.y < 30f) {
newBar.barValue.GetComponent<RectTransform>().pivot = new Vector2(0.5f,0f);
newBar.barValue.GetComponent<RectTransform> ().anchoredPosition = Vector2.zero;
}
}
}
}
Anyways, any help would be appreciated !
If you have any ideas on how to simplify the bar graph process, please let me know. Trying to write scripts to change UI elements is really difficult!
Thanks again!!!
Answer by Elthen · May 27, 2017 at 10:48 AM
I see you're doing something wrong, for example when you say: "Changer.GetComponent ();" you're trying to access the component Changer of Changer, but you said it's tied to EmptyObject4Script.
You should declare your variables at the beginning, something like:
private GameObject emptyObject4Script;
private Changer changerScript;
then in Start() you can assign a reference to your variables:
emptyObject4Script = GameObject.Find("EmptyObject4Script");
changerScript = emptyObject4Script.GetComponent<Changer>();
If you need to access your script, example:
Debug.Log (changerScript .myValues);
Hope I've been of help
Answer by MarkVincent · Oct 10, 2017 at 03:55 PM
I would like to thank you on behalf of the original poster. ;)