Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
avatar image
0
Question by robbyken · May 26, 2017 at 01:50 PM · getcomponentdynamicgraphnon-staticchart

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!!!

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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

Comment
Add comment · 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
0

Answer by MarkVincent · Oct 10, 2017 at 03:55 PM

I would like to thank you on behalf of the original poster. ;)

Comment
Add comment · 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.

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

Issue with calling a function from another script on trigger. Using JavaScript. 1 Answer

Using a String to populate GetComponent 1 Answer

Chart libarry like highcharts in Unity 1 Answer

Defining displayed 3D content from external source? 0 Answers

infinite 2d grid in an Editor Window 1 Answer


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