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 Nercoe · Oct 12, 2012 at 10:39 PM · multipleifstatement

|| inside if statement not working. (noob)

Hi guys, what I am trying to do is get the screen to fade out black when a life is lost. A life is lost by connecting with the trigger (this works fine and the fading code works). Unfortunately I cannot seem to get it to work for multiple values (I want it to fade every time a life is lost, not just ones). Any help is appreciated :)

 public var fadeOutTexture : Texture2D;
 public var fadeSpeed = 0.3;
 var lives = 3;
 
 private var alpha = 1.0;
 
 private var desiredAlpha = 0.0f;
 
 private var fadeOut = false;
 private var fadeDir = -1;
 
 function OnTriggerEnter(c:Collider)
     {
        if(c.gameObject.tag =="Player") 
            lives--;
                 fadeOut = true; 
       }
 
 
 function OnGUI(){
     if (fadeOut){
       GUI.color.a = alpha;
       GUI.DrawTexture(Rect(0, 0, Screen.width, Screen.height), fadeOutTexture);
                alpha += fadeDir * fadeSpeed * Time.deltaTime;
 }
 }
 
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 Noah-1 · Oct 12, 2012 at 10:42 PM

I'm not sure of understanding your question, but I will try to answer it:

|| stands for OR example:

 if(this || this){//do this}

try using && which stands for AND.

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 Nercoe · Oct 12, 2012 at 10:57 PM 0
Share

Yeah I tried it already but it didn't work :/ I am thinking maybe it's because I am asking it to repeat it multiple times (fade every time you die). Can the OnGUI function do this?

avatar image
0

Answer by echofiend · Oct 12, 2012 at 11:38 PM

Right now you have it only triggering when your lives count == 1. In order to do this you need to implement a function for when you lose a life that fades the screen

 public var fadeOutTexture : Texture2D;
 public var fadeSpeed = 0.3;
 var lives = 3;
 
 private var alpha = 1.0;

 private var desiredAlpha = 0.0f;
  
 private var fadeOut = false;
 private var fadeDir = -1;
 
 function OnTriggerEnter(c:Collider)
     {
        if(c.gameObject.tag =="Player") 
        RemoveLifeAndFadeScreen();//This function is added
        
     }
 
 function OnGUI(){
       //All of your alpha adjusting is done in Update() where most logic should be done
       //From here all of your desired alpha will be set, so you just apply it
       GUI.color.a = alpha;
       GUI.DrawTexture(Rect(0, 0, Screen.width, Screen.height), fadeOutTexture);
 }

 //This will fire everytime you lose a life
 function RemoveLifeAndFadeScreen(){
     lives--;
     fadeOut = true; 
        
 }
 
 }
 function Update()
 {
     //check to see if we should be fading
       if(fadeOut)
           if(alpha > desiredAlpha)//change this comparison to however you need it currently
                alpha += fadeDir * fadeSpeed * Time.deltaTime;
 }

Also you will need to decide how you want to handle the "FadeIn" because if you fade it out once it will remain that way. Unless you manually bring it back up. So you will need to have a check to see if alpha=0, and then decide if you want to immediately bring it to 1.0 or reverse your fadeout.

Comment
Add comment · Show 4 · 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 Nercoe · Oct 13, 2012 at 12:32 AM 0
Share

I updated the code.

It fades when the collider is hit, but only once. How would I go about implementing it to fade every time it's hit? Thanks for the reply.

avatar image echofiend · Oct 13, 2012 at 03:21 AM 0
Share

you would need to implement a function to reset the alpha whenever you think it should be... so if after lets say 5secs of black you make it light again, put a function in that says something of the sorts of if(whenTheyDie > 5 seconds){fadeToBlack = false; alpha = 1.0f;}

avatar image echofiend · Oct 14, 2012 at 12:05 PM 0
Share

Did this work for you... sorry im not going to write your entire algorithm for this, but I gave you the Jist of what you needed to do

avatar image Nercoe · Oct 14, 2012 at 01:30 PM 0
Share

It did not, and I never stated I wanted you to write it for me, in fact, that's the last thing I want. If you write it for me, I do not learn, simple. I appreciate the help but my university lecturer showed me the right ropes, thanks anyway.

avatar image
0

Answer by Bunny83 · Oct 12, 2012 at 11:33 PM

This won't work this way. Your "lives" variable is an integer so it holds a whole number (like 7, 2, 1, ...). When you do something like

 if(lives==1 || lives==2 || lives==3)

the body of the if will be executed as long as lives is 1, 2 or 3. It doesn't "detect" changes of the variable. OnGUI is called several times each frame and the if statement is evaluated each time.

If i get it right you want to fade the screen out when you lost a live. Since you want to fade it out multiple times it has to revert back to normal at some point. You didn't give much information on your actual procedure...

I guess your "fadeOutTexture" is the texture you want to actually "fade in" so the screen is hidded by the texture. In this case you have to slowly increase the alpha.

One way is to use a coroutine like this:

 public var fadeOutTexture : Texture2D;
 public var fadeSpeed = 0.3;
 var lives = 3;
 
 private var alpha = 1.0;  
 private var fadeDir = 1.0;
 
 
 function OnTriggerEnter(c:Collider)
 {
     if(c.gameObject.tag == "Player") 
     {
         lives --;
         alpha = 0.0f;
         fadeDir = 1;
         while(alpha < 1.0f)
         {
             alpha += fadeDir * fadeSpeed * Time.deltaTime; 
             yield null;
         }
         
         // Here the fade texture is completly visible
         // Do something and when you want to fade back in, just revert:
         fadeDir = -1;
         while(alpha > 0.0f)
         {
             alpha += fadeDir * fadeSpeed * Time.deltaTime; 
             yield null;
         }
         // here, the texture is invisible again
     }
 }
 
 function OnGUI()
 {
     if (alpha > 0.0f)
     {
         GUI.color.a = alpha;
         GUI.DrawTexture(Rect(0, 0, Screen.width, Screen.height), fadeOutTexture);
     }
 }
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

12 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

Related Questions

If \ Else how does the program reads it? 2 Answers

How to make an if statement with two conditions? 1 Answer

How can I perform a += on an if statement? 1 Answer

|| conditional statement not working 1 Answer

nested if statements doubt 2 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