Reputation: 13
I need to change the scale of an object based on a value from an array (data_int[]), it should increase if the value is increasing and vice versa. the code I tried does that but I can only visualize the end result. However, I need to visualize every step in the loop.
void Update()
{
if (MyFunctionCalled == false)
{
for (int i = 1; i < 25; i++)
{
if (data_int[i] > data_int[i - 1])
{
transform.localScale += new Vector3(0.01f, 0.01f, 0.01f);
}
else if (data_int[i] < data_int[i - 1])
{
transform.localScale += new Vector3(-0.01f, -0.01f, -0.01f);
}
}
MyFunctionCalled = true;
}
}
}
}
Upvotes: 0
Views: 848
Reputation: 1419
You can use a Coroutine
function to achieve your goal.
The line, yield return new WaitForSeconds(.5f)
will simulate waiting for .5 seconds before continuing. yield return null
, yield return new WaitForEndOfFrame()
, and others may also be used to delay the execution of a Coroutine
. More information about when each of these return may be found here. This question on coroutines may also be useful.
void Start()
{
StartCoroutine(ScaleObject());
}
IEnumerator ScaleObject()
{
for (int i = 1; i < 25; i++)
{
if (data_int[i] > data_int[i - 1])
{
transform.localScale += new Vector3(0.01f, 0.01f, 0.01f);
}
else if (data_int[i] < data_int[i - 1])
{
transform.localScale += new Vector3(-0.01f, -0.01f, -0.01f);
}
yield return new WaitForSeconds(.5f);
}
}
Upvotes: 2
Reputation: 15247
The whole loop is executed during 1 frame, you can't see the step by step. You can "simulate" the loop outside of the method Update
In example :
// initialize your iterator
private int i = 1;
// I removed the checks on MyFunctionCalled because this may be irrelevant for your question
void Update()
{
// use an if instead of a for
if (i < 25)
{
if (data_int[i] > data_int[i - 1])
{
transform.localScale += new Vector3(0.01f, 0.01f, 0.01f);
}
else if (data_int[i] < data_int[i - 1])
{
transform.localScale += new Vector3(-0.01f, -0.01f, -0.01f);
}
// this is the end of the supposed loop. Increment i
++i;
}
// "reset" your iterator
else
{
i = 1;
}
}
Upvotes: 1