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. :) + 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;
}
}
}
Here are some of the things you could do to optimize your code
You don't need to use GameObject.Find("Player") more than once.Ins$$anonymous$$d declare a variable to store the player gameobject and store it in Awake or Start method. Generally avoid using GameObject.Find in Update method.
Same goes for $$anonymous$$ana ,movespeed variable.Declare those globally so you can use them anywhere from your script.
i do try to avoid it, but here it only finds when pressed button,not even Get$$anonymous$$ey,just Get$$anonymous$$eyUp,.that's ok,is it not?:)+ yeah i tried to cache it,but then i had ,not in current context in update.
Answer by Hellium · Jan 22, 2018 at 11:05 AM
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. :) thank you J
Uhm it would help when you could cite the exact error message. It most likely tells you what is not available / accessible in this context. If it's the line you have mentioned it's either the baterieSlider
in PlayerVitals or the value
inside your slider.
Your answer
![](https://koobas.hobune.stream/wayback/20220612151040im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Upcast vs GetComponent 1 Answer
Unity 5.2 messing up car 0 Answers
How to get the component in a new class 0 Answers
Nav Mesh Problem with SetDestination 1 Answer