Hi,how to optimize this C# script,looks like mess to me. :) + doesn't work as it should;

Hi,how to optimize this C# script,looks like mess to me. :slight_smile: + doesn’t work as it should;
if it’s not that messy,how can i check if i have more than 5 manapoints? because now when i press B,nothing happens,…++ if i try to write Mana.baterieSlider.value = currentMana to update its says not in current context…

	private AudioSource source;
	public AudioClip hasteSound;

	public float countdown = 0;
	public float AirBasic = 30;

	public float currentMana = 0;


	bool isHaste;


	void Awake()
	{
		GameObject thePlayer = GameObject.Find ("Player");
		PlayerVitals Mana =  thePlayer.GetComponent<PlayerVitals> ();
		Mana.baterieSlider.value = currentMana;

		source = GetComponent<AudioSource> ();

	}

	void Update () {


		if (Input.GetKeyUp (KeyCode.B)&& !isHaste&& currentMana > 5)
			
		{
			GameObject thePlayer = GameObject.Find ("Player");
			PlayerVitals Mana =  thePlayer.GetComponent<PlayerVitals> ();
			inventory moveSpeed = thePlayer.GetComponent<inventory> ();

			isHaste = true;
			countdown = AirBasic;
			moveSpeed.speed += 1;
			Mana.baterieSlider.value -= 5;
			source.PlayOneShot (hasteSound, 0.9f);

		}
		if (isHaste == true) 
		{
			countdown -= Time.deltaTime;
		}
		if (countdown <= 0) 
		{
			//countdown = 0;
			isHaste = false;
			GameObject thePlayer = GameObject.Find ("Player");
			inventory moveSpeed = thePlayer.GetComponent<inventory> ();
			moveSpeed.speed = 4;

		}
	}
}

Try this:

private AudioSource source;
public AudioClip hasteSound;

public float countdown = 0;
public float AirBasic = 30;

public float currentMana = 0;


bool isHaste;

private GameObject player ;
private PlayerVitals mana;
private inventory inventory;

void Awake()
{
    player = GameObject.Find ("Player");
    if( player == null )
    {
        Debug.LogError("Can't find the player");
        enabled = false ;
        return ;
    }
    inventory = player.GetComponent<inventory> ();
    mana = player.GetComponent<PlayerVitals> ();
    mana.baterieSlider.value = currentMana;
    source = GetComponent<AudioSource> ();
}

void Update ()
{
    if ( !isHaste )
    {
        if( Input.GetKeyUp (KeyCode.B) && currentMana > 5)
        {
            isHaste = true;
            countdown = AirBasic;
            inventory.speed += 1;
            mana.baterieSlider.value -= 5;
            source.PlayOneShot (hasteSound, 0.9f);
        }
    }
    else
    {
        countdown -= Time.deltaTime;
        if (countdown <= 0)
        {
            isHaste = false;
            inventory.speed = 4;
        }
    }
}

yep i got it, i should’ve put the line 27(your code) in Update ,line 32(again in yours)
to me it said not in current context,.now the context is all over the place. :slight_smile: thank you J

yep i got it, i should’ve put the line 27(your code) in Update ,line 32(again in yours)
to me it said not in current context,.now the context is all over the place. :slight_smile: thank you J