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
Articoli simili
- How to make the background image of a button disappear when we click it, and make it appear on another button when we click it in Visual Studio
- What mobile games do you play daily and what keeps you coming back. If you could improve the game, what would you add?
- Is there any way in c to change the value of a global variable through a function without passing it to the function?
- What is the coolest thing you've done on your Android phone?