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
0
Question by united4life · May 08, 2013 at 06:44 AM · gameobjectblinking

Active and deactive Gameobjects.

When i click on a button i want to active and deactive gameobjects continously like a blink effect. This is the script i am using

 public GameObject cube;
     
     public bool isBlink;
     
     void Start () 
     {
         cube = GameObject.Find("Cube");
     }
     
     void Update () 
     {
         if(isBlink)
         {
             StartCoroutine("BlinkGameObject");
         }
     }
     
     void OnGUI()
     {
         if(GUI.Button(new Rect(100, 100, 100, 50), "Blink"))
         {
             isBlink = true;    
         }
     }
     
     IEnumerator BlinkGameObject()
     {
         yield return new WaitForSeconds(2f);
         
         cube.SetActive(!cube.activeSelf);
 }
 }

It is blinking but very fast. I want to slow it down like a normal blink effect.

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

4 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by whydoidoit · May 08, 2013 at 07:59 AM

Oh dear - so many answers to this question - all of them a bit wrong :S

So:

  • There is no need at all to start the coroutine from Update, you should start it from the GUI. Calling from Update just means you keep creating new copies and adds unnecessary spaghetti.

  • You should probably blink the renderer not the whole game object

        public GameObject cube;
      
         public bool isBlink;
      
         void Start () 
         {
            cube = GameObject.Find("Cube");
         }
      
        
         void OnGUI()
         {
            if(GUI.Button(new Rect(100, 100, 100, 50), "Blink"))
            {
              StartCoroutine(BlinkGameObject());
            }
         }
      
         IEnumerator BlinkGameObject()
         {
            if(isBlink) yield break;
            var cubeRenderer = cube.GetComponentInChildren<Renderer>();
            isBlink = true;
            while(isBlink)
            {
                 yield return new WaitForSeconds(2f);
                 cubeRenderer.enabled = !cubeRenderer.enabled;
            }
       
         }
    
        }
    
Comment
Add comment · Show 1 · 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 united4life · May 08, 2013 at 09:16 AM 0
Share

Thanks this helps a lot.

avatar image
1

Answer by VivekD · May 08, 2013 at 07:12 AM

Your BlinkGameObject() co-routine is being continuously called since isBlink always remains true after the first button press.I guess the code below will do for a blink effect :

     bool isBlink;
     float blinkTimer;
     void Start () 
     {
        isBlink = false;
        blinkTimer = 2.0f;
     }
  
     void Update () 
     {
        if(isBlink)
        {
              StartCoroutine("BlinkGameObject");
             isBlink = false;
        }
     }
  
     void OnGUI()
     {
        if(GUI.Button(new Rect(100, 100, 100, 50), "Blink"))
        {
          isBlink = true;  
        }
     }
  
     IEnumerator BlinkGameObject()
     {
            yield return new WaitForSeconds(blinkTimer);
  
            renderer.enabled = !renderer.isVisible;
         StartCoroutine("BlinkGameObject");
     }

Adjust blink timer according to your needs.

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 SubatomicHero · May 08, 2013 at 07:16 AM

in your BlinkGameObject() method I think by looking at it you need to add another line or two:

 IEnumerator BlinkGameObject()
 {
     isBlink = false;
     yield return new WaitForSeconds(2);
     cube.SetActive (!cube.activeSelf);
 
     // isBlink = true;
 }

I've commented out the last bit as I'm not sure if you need it. Whats happening is the update function is called many many times per second (hundred I believe) and when you click your gui button isBlink is true, whcih means update is constantly calling BlinkGameObject() over and over.

Hope this 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
0

Answer by CDK · May 08, 2013 at 07:55 AM

I am used to JS so this might be wrong but why do you have an "f" in WaitForSeconds? any way: Try doing this instead

  IEnumerator BlinkGameObject()
     {
        IsBlink = false;
        yield return new WaitForSeconds(50);
  
        cube.SetActive(!cube.activeSelf);
        IsBlink = true;
     }

the problem is that you are calling "BlinkGameObject" every frame. The wait only happens the first time you load it. This way it will be loaded, turn "IsBlink" off, so it doesn't load that again, and then do it's stuff and set "IsBlink" to true again.... Try it.. It's not tested but shoud work...

Good luck ;)

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

16 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

Related Questions

indestructible gameobject 0 Answers

Why are my 2D sprite GameObjects blinking and how do I fix this problem? 0 Answers

Blink objects at a constant rate 2 Answers

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

Use an objects (from array) position to focus a camera on 3 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