Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by CrucibleLab · May 28, 2015 at 03:54 PM · c#setactive

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.

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Multifred · May 28, 2015 at 04:11 PM 1
Share

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);

avatar image maccabbe · May 28, 2015 at 04:26 PM 1
Share

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.

avatar image CrucibleLab · May 28, 2015 at 04:27 PM 0
Share

@$$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.

avatar image Multifred · May 28, 2015 at 04:39 PM 0
Share

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);
      }
      //...
 }

5 Replies

· Add your reply
  • Sort: 
avatar image
2

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
2

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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();
 }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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 !!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

26 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges