- Home /
Is it possible to Invoke a function from a referenced variable?
I'm making a generic Pong game (pretty much replicating it with my own understanding of it) from scratch, and my idea of how the ball is destroyed and instantiated is as follows:
when the ball hits the Player2 goal collider (Player 1 gets the point), The ball's script calls it's function to destroy itself, and the GoalCollider script calls " game.spawnBallP1(); ", where game is the "game" is a reference to the GamePong script, which is pretty much the whole game managing script.
The GamePong's SpawnBallP1() function, only has "Instantiate(spawnBall, player1Launcher.transform.position, Quaternion. identity);"
I was wondering if there was a way to Invoke the instantiation of the ball, without having to make the "timer" by code.
Answer by BradyIrv · Jun 08, 2019 at 07:41 PM
You can actually use the keyword Invoke to achieve running a function after X amount of time:
// GoalCollider script call ball respawn
float timeToWait = 5f;
game.Invoke("SpawnBallP1", timeToWait);
// Function to call
public void SpawnBallP1()
{
Instantiate(ball, pos, rot);
}
Thanks a lot! Another thing I almost forgot to do was make the method public, and actually set the time to 0 >.
Your answer
Follow this Question
Related Questions
Making a timer that ends on a consistent update 2 Answers
Issue with Timed Shooting in C# 1 Answer
Multiple timers on server as objects or prefabs? 0 Answers
Same script on multiple objects. 2 Answers
Change directions, at random times? 1 Answer