1
\$\begingroup\$

I've created following code which works just fine:

GraphBar1.GetComponent<RectTransform> ().sizeDelta = new Vector2 (50, oScript.reactionTimeArray [0] * 1000);
GraphBar2.GetComponent<RectTransform> ().sizeDelta = new Vector2 (50, oScript.reactionTimeArray [1] * 1000);
GraphBar3.GetComponent<RectTransform> ().sizeDelta = new Vector2 (50, oScript.reactionTimeArray [2] * 1000);
GraphBar4.GetComponent<RectTransform> ().sizeDelta = new Vector2 (50, oScript.reactionTimeArray [3] * 1000);
GraphBar5.GetComponent<RectTransform> ().sizeDelta = new Vector2 (50, oScript.reactionTimeArray [4] * 1000);
GraphBar6.GetComponent<RectTransform> ().sizeDelta = new Vector2 (50, oScript.reactionTimeArray [5] * 1000);
GraphBar7.GetComponent<RectTransform> ().sizeDelta = new Vector2 (50, oScript.reactionTimeArray [6] * 1000);
GraphBar8.GetComponent<RectTransform> ().sizeDelta = new Vector2 (50, oScript.reactionTimeArray [7] * 1000);
GraphBar9.GetComponent<RectTransform> ().sizeDelta = new Vector2 (50, oScript.reactionTimeArray [8] * 1000);
GraphBar10.GetComponent<RectTransform> ().sizeDelta = new Vector2 (50, oScript.reactionTimeArray [9] * 1000);

As I'm re-writing almost the same code so many times, I'm convinced this can be done using less code, even though I'm confused on how to do it with the Array properties being present.

\$\endgroup\$
3
  • 1
    \$\begingroup\$ As we all want to make our code more efficient or improve it in one way or another, try to write a title that summarizes what your code does, not what you want to get out of a review. Please see How to get the best value out of Code Review - Asking Questions for guidance on writing good question titles. \$\endgroup\$ Commented May 11, 2016 at 13:00
  • \$\begingroup\$ Can you do a loop through the graphbars and refactor it to a single function? Sorry, I don't know this language. \$\endgroup\$ Commented May 11, 2016 at 15:52
  • \$\begingroup\$ @Raystafarian This is kinda the way I thought to do it - however I fail to do it, and thats where I would like the assistance =) \$\endgroup\$ Commented May 11, 2016 at 16:09

1 Answer 1

3
\$\begingroup\$

It could be written as :

var graphBars = new[] { GraphBar1, GraphBar2, GraphBar3, GraphBar4, GraphBar5, GraphBar6, GraphBar7, GraphBar8, GraphBar9, GraphBar10 };
for (int i = 0; i < graphBars.Length; i++)
    graphBars[i].GetComponent<RectTransform>().sizeDelta =
        new Vector2(50, oScript.reactionTimeArray[i] * 1000);
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.