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 /
avatar image
0
Question by guyschofield · Sep 07, 2016 at 04:47 PM · scripting problemif-statementsif statementnotifications

NOT condition not working

I'm having trouble getting a series of if statements to work: I think I must be misunderstanding something. I'm trying to enable/disable the renderers of different objects using a variable (it's for making sure that certain rooms and objects are hidden at different phases of the game. The statement 'if (phase == 1)' works fine and I can use 'if (phase ==2)' to disable the renderer. What I really want to do though is to disable them if phase is NOT equal to 1. I've tried 'if (phase != 1)' (see below) but that doesn't seem to work. I'd be dead grateful if anyone could tell me why! Cheers!

 void Update () 

 {
     GameObject door2 = GameObject.Find("room_2_door");//finding room2 objects and animators
     GameObject room2 = GameObject.Find ("room_2");
     Animator door2anim = door2.GetComponentInChildren<Animator>();
 


     if (phase == 1) //room 2 visible, 
     {
         
         door2anim.SetTrigger ("close");
         Renderer[] room2childrenrenderer = room2.GetComponentsInChildren<Renderer> ();
         foreach (Renderer r2cr in room2childrenrenderer) 
         {
             r2cr.enabled = true;
         }
     }
     if (phase !=1 ) // room 2 not visible
     {
         
         door2anim.SetTrigger ("go");
         Renderer[] room2childrenrenderer = room2.GetComponentsInChildren<Renderer> ();
         foreach (Renderer r2cr in room2childrenrenderer) 
         {
             r2cr.enabled = false;
         }
     }
Comment
Add comment · Show 1
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 NoseKills · Sep 07, 2016 at 04:58 PM 0
Share

Use Debug.Log in both ifs to make sure phase is what you think it is and that you enter the right if.

What do you mean by "doesn't work"? It gets done when not supposed to, doesn't get done when supposed to or both?

If you want something done when phase == 1 and something else done when it's not, you can simply do

 if (phase == 1) { 
     //something
 } else {
    //something else
 }

Another improvement I would suggest would be not to trigger animations and do all this 60 times every second in update, but ins$$anonymous$$d make a SetPhase() method that you'd use to change phase and do all of this only once whenever phase changes

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Blue-Cut · Sep 07, 2016 at 08:56 PM

First of all, you could simplify a bit your code to avoid mistakes. The code bellow does the same thing than your previous code. If you want to focus on your problem without changing your code, just look at the Update part.

As NoseKills said, check with the Debug.Log instruction that 'phase' is not equal to 1 :

  // Declare variables (data to keep all the time in your script)
     GameObject door2;
     GameObject room2;
     Animator door2anim;
     Renderer[] room2childrenrenderer;
     
     void Start()
     {
       // Get the value for each variables just once in Start
       door2 = GameObject.Find("room_2_door");
       room2 = GameObject.Find ("room_2");
       door2anim = door2.GetComponentInChildren<Animator>();
       room2childrenrenderer = room2.GetComponentsInChildren<Renderer> ();
     }
     
     void Update () 
     {
         // This is to print a line in the Unity console so you can check your values
         Debug.Log("phase = "+phase);
 
          // Set your trigger with a if/else
          if (phase == 1) 
          {
              door2anim.SetTrigger ("close");
          }
          else
          {
              door2anim.SetTrigger ("go");
          }
         foreach (Renderer r2cr in room2childrenrenderer) 
         {
             // Instead of true/false, you can affect directly your condition to avoid repeating
             r2cr.enabled = (phase == 1);
         }
     }

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 guyschofield · Sep 08, 2016 at 12:33 PM 0
Share

Great, many thanks @Nose$$anonymous$$ills and @Blue-Cut. I've tested whether the conditions are actually working and I think there's a problem elsewhere, which should be easier to solve. Thanks for the advice regarding declaring variables and where to do each bit!

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

84 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

Related Questions

mathf.abs(0) is higher than 90, why? 1 Answer

Unity Update is ignoring the GetKey part of my statement!? 2 Answers

How to put an if statement inside an if statement? 3 Answers

How to use or statements in an if statement. 1 Answer

If/else statement is always else 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