Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
This question was closed May 21, 2018 at 10:27 PM by polan31 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by polan31 · May 21, 2018 at 12:01 PM · coroutineienumeratorappear

Why doesn't the object appear?

I have a "FishStar" object.

I would like to turn it off ("FishStar") for 2 seconds after the collision with the "Player" object and then turn it on again.

I use Coroutine for this.

Where is the mistake?

After adding the script (to "FishStar"), I put the object in the "otherGameObject" tab, but the object after disappearing does not want to appear again.

It's weird because if I add another object instead of otherGameObject tab (in Inspector), it works perfectly.

    using System.Collections;
   using System.Collections.Generic;
   using UnityEngine;
  
   public class FishStarRotation : MonoBehaviour {
   
   public float speed;
   private Rigidbody2D rb2D;
   public GameObject Bonus;
  
  
   // Use this for initialization
   void Start () {
  
       Bonus = GetComponent<GameObject>() ;
          
   }
   
   // Update is called once per frame
   void Update () {
  
       transform.Rotate (0, 0, speed);
   }
   void OnCollisionEnter2D(Collision2D coll){
       if (coll.gameObject.tag == ("Player"))
           StartCoroutine ( DIS ());
       
   }
  
   IEnumerator DIS ()
   {
       Bonus.SetActive(false);
       yield return new WaitForSeconds(2f);
       Bonus.SetActive(true);
   }
  

}

Comment
Add comment
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

2 Replies

  • Sort: 
avatar image
0
Best Answer

Answer by polan31 · May 21, 2018 at 10:26 PM

I solved the problem.

Correct working script :

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class FishStarRotation : MonoBehaviour {
     
     public float speed;
     private Rigidbody2D rb2D;
 
 
 
     // Use this for initialization
     void Start () {
         
     }
     
     // Update is called once per frame
     void Update () {
 
         transform.Rotate (0, 0, speed);
     }
     void OnCollisionEnter2D(Collision2D coll){
         if (coll.gameObject.CompareTag("Player")) {
             StartCoroutine (speedUp ());
             //gameObject.SetActive (false);
             GetComponent<SpriteRenderer> ().enabled = false;
             GetComponent<PolygonCollider2D> ().enabled = false;
             // Above line will deactivate the first collider it will find.
         }
     }
 
         IEnumerator speedUp ()
         {
             Debug.Log("before");
             yield return new WaitForSeconds(2f);
             Debug.Log("after"); 
         GetComponent<SpriteRenderer> ().enabled = true;
         GetComponent<PolygonCollider2D> ().enabled = true;
         }
 }
 
 
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 greencheese122 · May 21, 2018 at 12:06 PM

You are disabling the object The script is on,so script stopps running and object don't get enabled Again.Instead of disabling object just disable its collider and renderer with getComponent

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

Follow this Question

Answers Answers and Comments

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

How to force IEnumerator methods to be called only with StartCoroutine 2 Answers

Could not load source 'Coroutines.cs': No source available. 1 Answer

Help with coroutine and IEnumerator on condition 0 Answers

Unity Editor can't find IEnumerated 2 Answers

My code is not working - HELP - IEnumerator Coroutines 0 Answers


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