Firstly, I know there are LOTS of questions about this particular method and how any given object can be made active and inactive with it. Based on what I learned from those questions (and answers), I wrote the following code that in essence is to deactivate my Player object and reactivate it after a few seconds.
private GameObject playerObj; // keeping a reference to the player object
void Start() {
playerObj = transform.gameObject; // although this script itself is already attached to the player object, a separate reference is being made
....
}
void OnCollisionEnter(Collision other) {
if (other.gameObject.CompareTag("Enemy")) {
StartCoroutine(DieByEnemy());
}
....
}
IEnumerator DieByEnemy() {
playerObj.SetActive(false);
yield return new WaitForSeconds(3);
playerObj.SetActive(true);
SpawnPlayer();
}
So, when the player collides with the enemy, the ‘DieByEnemy’ coroutine is called and there is a 3-second gap between when the player object gets “turned off” and “turned on”.
As commented in the above code, this script itself is already attached to the player object. And the way I had it initially was as follows:
transform.gameObject.SetActive(false);
yield return new WaitForSeconds(3);
transform.gameObject.SetActive(true);
And this apparently doesn’t work because once the object is deactivated, the script no longer has a reference to the same object and according to numerous forum users, that’s why keeping a separate reference to the object should work.
But in my case, it still doesn’t work. The player gets deactivated (or disappears) just fine as intended. But it doesn’t reappear after the specified time. And there’s no error (e.g., nullpointerexception) at all. I’d bet some of you are thinking, ‘Why don’t you just turn off the Mesh renderer?’, but that’s not an acceptable workaround in my case as it introduces other issues which I won’t elaborate here.
Well for starters, SetActive disable all updates and Coroutines. So basically what you are doing is telling your object to stop updating and then wait for it to update.
You should separate the script from the object you want to toggle and create a reference to your object as a public or serialized variable.
This way your coroutine will remain active and your object state will toggle as intented.
Hi, I recently ran into this issue myself.
This isn’t working because when you disable your game object, you’re disabling all components and scripts attached to it. The attached scripts stop running completely. Multiple internal references will still all be disabled because they are part of the disabled object. What you need is an outside reference from another gameobject with it’s own script to manage the re-enabling of this game object.
Hope that helps.
An alternative to passing control to another object, is to use the fact that deactivating an object does not affect pending calls set up by Invoke. I think that something along these lines should work…
IEnumerator DieByEnemy()
{
Invoke("ActivateAndSpawnPlayer", 3);
playerObj.SetActive(false);
}
void ActivateAndSpawnPlayer()
{
playerObj.SetActive(true);
SpawnPlayer();
}
NOT Inititialize Gameobject in void Start
Initialize in Public variable.
2 Hrs… later
ERROR
private GameObject espada;
void Start () {
espada = GameObject.Find ("Arma_Espada");
}
OK
public GameObject espada;
void Start () {
//Add Gameobject from Inspector
//espada = GameObject.Find ("Arma_Espada");
}
Voila !!
There is an EASY an simple way
-
You just need to deactivate the Mesh Renderer!!
-
IEnumerator<float> FlashHit() {
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
gameObject.GetComponent<MeshRenderer>().enabled = false;
}
yield return Timing.WaitForSeconds (invTime/8);
for (int k = 0; k < 4; k++) {
gameObject.GetComponent<MeshRenderer>().enabled = true;
}
yield return Timing.WaitForSeconds (invTime/8);
}
}
- This code will make the object blink a few times
- Maybe you will need to deactivate the collider too
Hope it works