Is there a better way to scale a game object in an Update Method

Hello, I am unsure whether or not I should change my codes around because computation or whatever might get gobbled up because of the update is checking something. In my game I am trying to scale a game object that is a bar of energy in the update method that constantly adds .5 increments. And within the update method is an if statement that if the ‘energy’ were to pass 100, the new energy value becomes 100. But the increments continues to add .5 to the already existing 100 so it goes back to the 100 value once its passed 100. I feel this to be like a vicious cycle of checks that takes up computer computation/compiling or whatever.

I’ve looked up the documentation about update and fixed update and the only difference I could pick up was that fixedupdate has more to do with physics and rigidbody.

Here is the code so that it would be easier to see what I am talking about:

using UnityEngine;
using System.Collections;

public class AlternateAttackProtocol : MonoBehaviour 
{
	private static AlternateAttackProtocol instance = null;
	public static AlternateAttackProtocol Instance
	{
		get {return instance; }
	}

	public Transform alternateAttackDisplay;
	public float maxAlternateAttack = 100.0f;
	public float currentAlternateAttack;
	private float alternateAttackOriginalXScale;

	private GameObject player;

	
	void Start () 
	{
		alternateAttackOriginalXScale = alternateAttackDisplay.localScale.x;
		currentAlternateAttack = 0.0f;
		player = GameObject.FindGameObjectWithTag ("PlayerCharacter");
	}

	void Update () 
	{
		currentAlternateAttack = currentAlternateAttack + 0.5f;

		if(currentAlternateAttack >= 100)
		{
			Debug.Log("Being Accessed");

			currentAlternateAttack = 100;
			
		}

		if (currentAlternateAttack <= 100 && Input.GetButtonDown ("Fire2")) 
		{
			currentAlternateAttack = 0.0f;
			player.SendMessage ("ShootAlternativeStandBy", true);
			Debug.Log ("AlternateAttackButton has been Reached");
		}

		alternateAttackDisplay.localScale = new Vector3(alternateAttackOriginalXScale * (currentAlternateAttack/maxAlternateAttack), alternateAttackDisplay.localScale.y, alternateAttackDisplay.localScale.z);

	}
}

This has been bothering due to me not wanting to have a game that has performance issues. If anyone can shed some light on this that would be awesome. And Thanks.

One excess condition as Update() won’t reduce productivity of your application. But the first, the increase in “currentAlternateAttack” is bound not at the right time, and to frame per second (as Update() there is a simple addition with 0.5). Perhaps, you need to make a binding at the right time. And the second, you can add Coroutine a method, then the increase will be executed only to 100. The example is lower:

 //Add one variable for detect Coroutine
 private bool isAlter = false;

 void Start () {
  alternateAttackOriginalXScale = alternateAttackDisplay.localScale.x;
  currentAlternateAttack = 0.0f;
  //Starting coroutine for increment alternative attack
  StartCoroutine(this.addCurAlt()); //or use StartCoroutine("addCurAlt");
  player = GameObject.FindGameObjectWithTag ("PlayerCharacter");
 }

 //Add function for scale alternative attack display
 public void myScaleAlter() {
  alternateAttackDisplay.localScale = new Vector3(alternateAttackOriginalXScale * (currentAlternateAttack/maxAlternateAttack), alternateAttackDisplay.localScale.y, alternateAttackDisplay.localScale.z);
 }

 //Create coroutine
 public IEnumerator addCurAlt() {
  isAlter = true;
  //First, show right display
  this.myScaleAlter();
  yield return null;
  while(currentAlternateAttack < 100) {
   currentAlternateAttack = currentAlternateAttack + 0.5f;
   this.myScaleAlter();
   yield return null;
  }
  isAlter = false;
  yield return null;
 }

 void Update () {
  //Maybe, use only full charge alternative attack? Maybe, (currentAlternateAttack == 100)
  if (currentAlternateAttack <= 100 && Input.GetButtonDown ("Fire2")) {
   currentAlternateAttack = 0.0f;
   player.SendMessage ("ShootAlternativeStandBy", true);
   Debug.Log ("AlternateAttackButton has been Reached");
   if(!isAlter) {
    StartCoroutine(this.addCurAlt());
   }
  }
 }

Of course, you use invoke repeat too. I hope that it will help you.