• 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 ItArth · Jan 31, 2020 at 01:14 AM · scripting problemimage effects

Add a component 1 time in LateUpdate

I have the GrayScale Image Effect, that put all the screen white and black, so i have too in my LateUpdate void that when my Life is lower that 16 I add the GrayScale effect to the MainCamera, but this GrayScale is added infinitely how i can add it 1 time? this is my Script:

         void LateUpdate()
         {
             LowLife();
         }

  void LowLife()
         {
             Grayscale grayscale;
 
             if (currentHealth <= lowLife)
             {
                 Camera.main.gameObject.AddComponent<Grayscale>(); // Add the GrayScale
 
                 if (!lowLifeSource.isPlaying && !dead)
                 {
                     lowLifeSource.clip = heartClip;
                     lowLifeSource.Play();
                 }
             }
             else
             {
                 if (!dead)
                 {
                     
                     lowLifeSource.Stop();
                 }
             }
         }




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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by IINovaII · Jan 31, 2020 at 03:22 AM

Running GetComponent() method during every frame is a terrible idea. Instead, introduce a bool field to your script which changes its state depending on if GrayScale is added or not. The code will look something like this.

 private bool _isGrayScaleAdded;
 
 void LateUpdate()
 {
     LowLife();
 }
 void LowLife()
 {
      Grayscale grayscale;
 
      if (currentHealth <= lowLife)
      {
      
         //This part checks if you've already added it or not.
         if(!_isGrayScaleAdded){
             if(Camera.main.gameObject.GetComponent<GrayScale> == null)
                 Camera.main.gameObject.AddComponent<Grayscale>(); // Add the GrayScale
             _isGrayScaleAdded = true; //Changing this value to true makes sure it won't be added again and also makes sure GetComponent method won't be called again
         }
          
 
          if (!lowLifeSource.isPlaying && !dead)
          {
              lowLifeSource.clip = heartClip;
              lowLifeSource.Play();
          }
      }
      else
      {
          if (!dead)
          {
              
              lowLifeSource.Stop();
          }
      }
 }
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 logicandchaos · Jan 31, 2020 at 03:12 AM

if(Camera.main.gameObject.GetComponent()==null)
Camera.main.gameObject.AddComponent();

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 Fariborzzn · Jan 31, 2020 at 03:09 AM

try this:

     if(Camera.main.gameObject.GetComponent<Grayscale>()==null)
     {
        Camera.main.gameObject.AddComponent<Grayscale>();
     }




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 logicandchaos · Jan 31, 2020 at 03:12 AM 0
Share

hey that was my answer.. I was too slow :(

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

214 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 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 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 accurately map UV from second texture in image effects 0 Answers

How to get real-time AO for mobile platforms? 0 Answers

A Javascript trying to access an image effect C# script 1 Answer

How do you change text after highlighting a specific image via script? 0 Answers

"Can't add script behaviour VisualContainerAsset. The script needs to derive from MonoBehaviour" - from 2d rouglike tutorial 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