I’m still pretty new to Unity, but if there’s an answer to this, I haven’t seen it yet.
I’m trying to use Invoke to give a slight delay to re-instatiate my player object after it dies, but no matter how I try I cannot get the Invoke method to actually execute.
Here’s the code:
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
public class GameController : MonoBehaviour
{
public static int lives;
public LifeCounter lifeCounter;
private LevelManager levelManager;
// Use this for initialization
void Start(){
lives = 2;
MakePlayer();
}
void MakePlayer() {
Instantiate(Resources.Load("Player"));
}
public void OneUp(){
lives++;
}
public void PlayerDie(){
Destroy(GameObject.Find("Player(Clone)"));
lives -= 1;
lifeCounter = GameObject.Find("LifeCount").GetComponent<LifeCounter>();
lifeCounter.LoseLife();
Debug.Log(lives);
if (lives >= 0){
Invoke("MakePlayer", 2);
}else {
//Game Over
levelManager = GameObject.Find("LevelManager").GetComponent<LevelManager>();
Cursor.visible = true;
levelManager.LoadLevel(SceneManager.sceneCountInBuildSettings - 1);
}
}
}
If I use a regular call to re-instantiate the player, it works fine, and if I use and Invoke for the initial call it works fine too, but I can’t figure out how to make it do what I actually want.
Thanks in advance for any answers you can give me.
Try SendMessage instead if it's already on the same object? Maybe?
– UNDERHILLAre you getting any error in console ? put
– itsharshdeepDebug.Log("SomeMessage")and test the call sequences or by putting breakpoints.