if within an if statement?

hey all, so for my score system im trying to get 1 to 4 berrys pop up on screen depending on the score. all is good so far and when the one berry appears it grows in scale until it hits a max scale, then it starts to shrink down again, what i want it to do is when its shrinking down, when it reaches a final scale, i want it to stop. but so far it just keeps shrinking back to zero =s

any help would be great

ScoreBerry01.transform.localScale = Vector3.one * BerryScale;
		BerryScale += BerryGrowthRate * Time.deltaTime;
		
		if( BerryScale > BerryMaxSize)
		{
			BerryGrowthRate = -10.0f;
			
			if(BerryScale < BerryFinalSize)
			{
				BerryGrowthRate = 0.0f;
			}
		}

So, if I’m right, what you want to happen is that the berry grows to the maximum size, then starts shrinking until it hits the final size, then stops?

Okay, so, first of all, you’ll need to learn the skill to read your programs - not just what you write, but as the computer reads them. Remember, it executes from the top to the bottom every frame, and it will only progress into an if block if the condition is set. This is why your first attempt didn’t work, because the if statement: if(BerryScale < BerryFinalSize) was only executed when the BerryScale variable was greater than BerryMaxSize, as per the outer if statement.

The reason this second one doesn’t work is that you have no condition to stop it from occurring immediately, as you’ve already said you realise.

So, what you really need is a marker, or flag, to notify the program once it has finished the first part (namely, once the Berry hits the max size). This is a standard way of segmenting actions, and is very simple. All you need is a single, boolean variable. Booleans are incredibly useful. They only store a single value - true or false, but they are the key to just about all logic you will ever need. Boolean flags are particularly useful, as you can set one when an event occurs, and then check it later when you need to verify whether that event occurred… sounds like the sort of thing you need to do, right?

So, you’ll need to make a new variable, of type bool. Since you’re only ever going to use it inside the class, it’s best to make it a private variable (there are 3 main access modifiers in C# (there are more, but I won’t go into them), private, protected and public, in order. A private variable can only be seen inside the current class, protected variables can be seen inside the current class and its children, and public variables can be seen anywhere. In general, you want to use the access modifier that lets the fewest things possible ‘see’ the variable, as it makes your program more secure. If no access modifier is given, variables are assumed to be private). I’d call this variable something like berryMaxed (on another note, this form of capitalisation, with the first letter being lowercase, is good practice for variable names, as it distinguishes them from Class names, which usually begin with a capital letter).

Having made your new variable, you simply need to do two things. The first is to set it to true when the berry hits the maximum scale:

if( BerryScale > BerryMaxSize)
{
    BerryGrowthRate = -10.0f;
    berryMaxed = true;
}

Now all you need to do is test it before you set the final size of the berry:

if(berryMaxed && BerryScale < BerryFinalSize)
{
    BerryGrowthRate = 0.0f;
    BerryScale = BerryFinalSize;
}

&& is the logical “AND” operator, meaning the if statement will only execute if both of those conditions are true. For boolean values, something like berryMaxed is equivalent to writing berryMaxed == true. Now, why does this work? Well, when the code comes round on the first frame, it finds that BerryScale is less than BerryMaxSize, so it skips the first if statement entirely. Now, it looks at the second one. BerryScale is smaller than BerryFinalSize at this point, but it actually doesn’t even check that, because it finds that berryMaxed is false, so it skips the second if statement, too. It repeats this a number of times, until BerryScale is larger than BerryMaxSize. Then, the first if statement executes. Now, berryMaxed is true, so it can progress past the first condition in the second if statement, and start checking the second one. Eventually, it finds that BerryScale is less than BerryFinalSize, and it executes the second if statement, exactly as you want it to.

First of all, I would have a Berry script on each berry. The Berry script has whatever functionality a berry has, in this case a growing/shrinking functionality. To me, it doesn’t make much sense to have the User interface control a Berry.

That grow/shrink functionality would be best implemented as a coroutine that does one grow/shrink cycle. If more than one cycle is required, the coroutine can either call itself in the end or have an InvokeRepeating call a function that starts the coroutine.

Something like:

public class Berry{
void Start(){
    StartCoroutine(GrowShrinkCycle());
}

private IEnumerator GrowShinkCycle(){
    if(transform.localScale != Vector3.one)
        yield return break; //we're already running

    Vector3 startScale = Vector3.one;
    Vector3 endScale = new Vector3(10f,10f,10f);
    float cycleTime = 1f; //it will grow and shrink for 1 second
    float timeSpent = 0f;

    //grow the berry
    while( timeSpent < cycleTime)
    {

        transform.localScale = Vector3.Lerp( startScale, endScale,(timeSpent/cycleTime) );
        timeSpent += Time.deltaTime;
        yield return null;
    }

    timeSpent = 0f;

    //shrink the berry
    while( timeSpent < cycleTime)
    {
        transform.localScale = Vector3.Lerp( endScale, startScale,(timeSpent/cycleTime) );
        timeSpent += Time.deltaTime;
        yield return null;
    }
    transform.localScale = Vector3.one;
}
}

When evaluating code like this, you have to think about the value it will have each time through the loop. So the BerryScale is greater than max BerryMaxSize, and you change the BerryGrowthRate to -10. Now the berry start to shrink. It will no longer be greater than BerryMaxSize, so the code inside the ‘if’ statement will no longer be executed. Assume the initial size of the berry is larger than ‘BerryFinalSize’, you can just move the inner ‘if’ statement:

       ScoreBerry01.transform.localScale = Vector3.one * BerryScale;
       BerryScale += BerryGrowthRate * Time.deltaTime;
 
       if( BerryScale > BerryMaxSize)
       {
         BerryGrowthRate = -10.0f;
       }
       else if(BerryScale < BerryFinalSize)
       {
          BerryGrowthRate = 0.0f;
       }