- Home /
Setting 'SetActive' to TRUE not working
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.
Hmmm basicaly you are deactivating the GameObject that is responsible for the respawn... well it is deactivated, that's why.
I suggest you to create another GameObject on your scene "respawner":
public class Respawner: $$anonymous$$onoBehaviour
{
public void Respawn(GameObject pTarget)
{
StartCoroutine(DieByEnemy(pTarget));
}
IEnumerator DieByEnemy(GameObject pTarget) {
pTarget.SetActive(false);
yield return new WaitForSeconds(3);
pTarget.SetActive(true);
pTarget.Send$$anonymous$$essage("SpawnPlayer");
}
}
and in your player's script you reference the Respawner object and call myRespawner.Respawn(this.gameObject);
The reason a gameobject shouldn't disable then enable itself is because the Update (and by extension Coroutines) aren't called for scripts of disabled objects. I haven't read or observed anything that indicates that disabling an object changes any references to it. If you plan on re enabling an object using Update or Coroutine you need to do it from another gameObject's script, as in $$anonymous$$ultifred's comment. I believe that Invoked statements are not affected by a GameObject being enabled/disabled so you can try to make a timed your gameObject using Invoke.
@$$anonymous$$ultifred - I'm afraid I'm not following you here. I get the "create another gameobject" part, but what would I do with the original Player object then? Also, am I to deal with only the "respawner" object throughout the deactivate & reactivate process? Would appreciate it if you could elaborate it a bit more.
in your player's script:
//...
void Start() {
myRespawner = new GameObject("respawner", typeof(Respawner)) ;
}
void OnCollisionEnter(Collision pOther) {
if (pOther.gameObject.CompareTag("Enemy")) {
myRespawner.Respawn(this.gameObject);
}
//...
}
Answer by alexandre-fiset · May 29, 2015 at 09:18 AM
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.
Answer by DanGoldstein91 · May 29, 2015 at 12:28 AM
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.
Answer by Bonfire-Boy · May 29, 2015 at 09:36 AM
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();
}
Answer by sysmaya · Nov 09, 2016 at 01:01 PM
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 !!
Answer by OusedGames · Nov 09, 2016 at 03:51 PM
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
Your answer
![](https://koobas.hobune.stream/wayback/20220613200400im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Game optimisation using SetActive partially ineffective. 1 Answer
Deactivating GameObject problem 2 Answers
Reenabling GameObject with SetActive - NullReferenceException 1 Answer