Casa > I > In Unity, How Do You Make A Function From A Different Script Happen After Another One Is Done?

In Unity, how do you make a function from a different script happen after another one is done?

You could first make a Boolean, let's say isDone. Now we could make a function, let's say firstOne. Now in the update method, we do in an if statement:

using UnityEngine;

class firstScript : MonoBehaviour {

void firstOne()

{

}

void Update

{

if (isDone){

firstOne();

}

}

}

What this does is that it calls firstOne method ONLY if the bool isDone becomes true. Also, make sure it is in the update method. Now, this signifies that when the bool isDone becomes true, the method is called as well. Then, we can have a reference to a game object where the other script is sitting, let's call that script secondScript. Inside the second script, let's have a method called laterMethod. So it goes

using UnityEngine;

class secondScript : MonoBehaviour

{

public void laterMethod()

{

print (“The later method has been called”);

}

}

Make sure that laterMethod is public so that you can access it. In the first script, now the code is

using UnityEngine;

class firstScript : MonoBehaviour

{

public GameObject secondScriptSittingOn;

public bool isDone;

void Update()

{

if(isDone)

{

firstMethod();

secondScriptSittingOn.GetComponent(secondScript.laterMethod();

}

}

void firstMethod()

{

print(“First Method has been called”);

}

}

This is the only way I found, hope you like it. You could also use IEnumerators but that'll be a bit complex and also I'm on mobile so duh

De Gollin Addleman

Existem alguns tutoriais para construir um jogo como o Temple Run usando o motor de jogo Unity3D, ou qualquer outro motor de jogo? :: Qual é um bom conselho para um recém-chegado ao iOS do Android?