Question by
Okiski · Oct 21, 2017 at 03:50 PM ·
update functionupdate problemhow toonceonly
How to call a function in update only once (C#)
void Update () {
timeLeft -= Time.deltaTime;
if (timeLeft < 0f && timeLeft > -1f)
{
rend.material.color = kirmizi;
if (Input.GetButton("Fire1") && rend.material.color == kirmizi)
{
rend.material.color = Color.green;
timeLeft = 10f;
}
}
if (timeLeft < -1f)
{
rend.material.color = Color.blue;
if (Input.GetButton("Fire1") && rend.material.color == Color.blue)
{
rend.material.color = Color.black;
}
}
}
When timeLeft gets < -1f material gets blue and while I am holding fire1 material gets black but when I stop holding fire1 material gets blue again ! How can I keep it black ?
Comment
Best Answer
Answer by djenningsais · Oct 21, 2017 at 04:18 PM
@Okiski -
If you want your color to stay black you can add a class level boolean variable to keep track of whether black has been set. Then wrap the set blue in an if statement regarding that boolean variable.
Your answer
![](https://koobas.hobune.stream/wayback/20220612141535im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Do Something Once 1 Answer
C# Update Function Question 2 Answers
Variables not being updated with get and set [SOLVED] 2 Answers
Unity - AdMob - Video reward only shows once. 2 Answers
Game crashing when im trying to use Inapp update(Unity) 0 Answers