Stufle
April 29, 2020, 5:59am
1
I have this code
public float speed;
// Start is called before the first frame update
void update()
{
if (Score.score > 2)
{
speed = 4;
KillPipes();
}
if(Score.score > 50)
{
speed = 5;
}
if(Score.score > 100)
{
speed = 6;
}
}
I only need this to run once, and then delete or disable the code, how can I do that?
If you put the update function in a script and that script does not get destroyed after the first time it runs, it will continue. I think you are potentially looking for behaviour that can be provided by a coroutine:
void Start(){
StartCoroutine(OneUpdate());
}
IEnumerator OneUpdate(){
yield return new WaitForEndFrame();
//Your logic
}
It is not clear what the question asks for.
The Start() method runs only once anyway.
If you want to destroy the script after execution you can write:
void Start(){
//Start Code
Destroy(this); //Destroys this script
}
If you want to destroy the gamObject after execution write:
void Start() {
//Start Code
Destroy(gameObject);
}
musap
April 29, 2020, 11:08pm
4
You can use enabled = false but it will disable the script and the update function will not be called unless you enable it again by another script.
private void Update()
{
if (score > 2)
{
speed = 4;
KillPipes();
enabled = false;
}
// This code will not reach beyond this line
if (score > 50)
{
speed = 5;
}
if (score > 100)
{
speed = 6;
}
}