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 epic1legend · Apr 14, 2014 at 08:43 PM · reloadingand

if( ... && ... && ... || ... &&) { reload }

So I have this code for my reload function

 if(Input.GetKeyDown("r") && magAmmo < magCap && ammo != 0 && reloading == false && zip.GetComponent(HangOnObject).hanging == false && player.GetComponent(JumpAnimate).jumping == false){
             reloading = true;

but if I go into another scene reload won't work because zip no longer exists. I tried

 if(Input.GetKeyDown("r") && magAmmo < magCap && ammo != 0 && reloading == false && zip.GetComponent(HangOnObject).hanging == false || GameObject.Find("Connect") == null && player.GetComponent(JumpAnimate).jumping == false){
             reloading = true;

but when I go into another scene he won't stop reloading. What can I do?

edit: I tryed this also

 if(Input.GetKeyDown("r") && magAmmo < magCap && ammo != 0 && reloading == false && zip.GetComponent(HangOnObject).hanging == false && player.GetComponent(JumpAnimate).jumping == false
             || Input.GetKeyDown("r") && magAmmo < magCap && ammo != 0 && reloading == false && GameObject.Find("Connect") == null && player.GetComponent(JumpAnimate).jumping == false){
             reloading = true;

still won't reload on other scenes.

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 Benproductions1 · Apr 14, 2014 at 09:32 PM 0
Share

It's generally never a good idea to have && and || operators in a single row. I would wrap your conditions in brackets so it's much more obvious which conditions do what.

4 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by jonSG · Apr 14, 2014 at 09:06 PM

There are lots of optimization opportunities here, but it might be easiest to do something like this until you have it all under control.

 if ( Input.GetKeyDown("r") && canReload() ) {
     reloading = true
     print("we are reloding...");
 }

 private bool canReload(){
     // can't reload if we are already reloading
     if ( reloading ) { return false; }
 
     // can't reload if we are out of ammo
     if ( ammo == 0 ) { return false; }
 
     // can't reload if we fail some "mag test"
     if ( magAmmo >= magCap ) { return false; }
 
     // can't reload if we are jumping
     if ( player.GetComponent(JumpAnimate).jumping ) { return false; }
 
     // can't reload if we are on a zip line
     if (
         zip != null &&
         zip.GetComponent(HangOnObject) != null &&
         zip.GetComponent(HangOnObject).hanging ) { return false; }
 
     // Looks like we can reload
     return true;
 }
Comment
Add comment · Show 3 · 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 Benproductions1 · Apr 14, 2014 at 09:37 PM 0
Share

You don't need != null. A reference type is automatically considered as false if it references null.
It would also be more efficient if you got the HangOnObject once before the condition, rather than twice during.
The JumpAnimate component can also be cached.

avatar image jonSG · Apr 15, 2014 at 01:47 AM 1
Share

The conditional check is complicated so my advice is to make it as straight forward as possible before attempting something clever.

In my opinion testing for a null is much clearer than testing to see if something can be cast to bool.

Of course it would be more efficient to NOT GetComponent() multiple times a frame. That was not the point.

avatar image epic1legend · Apr 16, 2014 at 12:51 AM 0
Share

Thanks a lot! This helped me make it work.

avatar image
1

Answer by perchik · Apr 14, 2014 at 08:57 PM

First off, its a very bad idea to use GetComponent every frame; store it on start and access it every frame.

Second, here's your code with line breaks so I can refer to it easier:

 if(Input.GetKeyDown("r") 
     && magAmmo < magCap 
     && ammo != 0 
     && reloading == false 
     && zip.GetComponent(HangOnObject).hanging == false 
     && player.GetComponent(JumpAnimate).jumping == false)
 {
     reloading = true;
 }
     
     
 if(Input.GetKeyDown("r") 
     && magAmmo < magCap 
     && ammo != 0 
     && reloading == false 
     && zip.GetComponent(HangOnObject).hanging == false 
     || GameObject.Find("Connect") == null 
     && player.GetComponent(JumpAnimate).jumping == false)
 {
     reloading = true;
 }


Now, you say that zip no longer exists. If you know that for sure, why use line 16 at all? I think the OR is definitely the problem because I have no idea how it would handle that. Typically you'd combine clauses in parentheses with an OR to handle specific cases:

(A && B && C) OR (!A && D)


My overall suggestion though, get all the components at start and then figure out your logic. I'd suggest something like this :

 HangOnObject hanger ;
 JumpAnimate jumper ;
 GameObject connector;
 public bool hasZip;
 bool hasConnector;
     
 
 void Start(){
 
     if(hasZip){
         hanger = zip.GetComponent<HangOnObject>();
     }
     jumper = player.GetComponenet<JumpAnimate>();
     connector = GameObject.Find("Connect");
     if(connector == null) hasConnector = false;
 }
 
 void Update(){
     if(Input.GetKeyDown("r"){
         if( magAmmo < magCap // player isn't already loaded
             && ammo !=0  // there's ammo left to use
             && !reloading //I'm not already reloading
             && ((hasZip && !hanger.hanging) //zip exists and im not hanging
                  || (!hasZip && hasConnector))
             && !jumper.jumping //I'm not mid-jump
         ){
             reloading = true;
         }        
     }
 
 }
 
Comment
Add comment · Show 2 · 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 epic1legend · Apr 14, 2014 at 09:08 PM 0
Share

I didn't figure it out but this helped me organize a lot, so thanks.

avatar image perchik · Apr 14, 2014 at 09:11 PM 0
Share

updated with a condition about having the zip component...but I like @jonSG's answer better ( except you should still get the components at the beginning)

avatar image
0

Answer by MasterPDON · Apr 14, 2014 at 09:08 PM

You should try introducing brackets to your multiple conditional statements to show 'priority'.

2*3+2-5

(2*3)+(2-5)

2*(3+2)-5

The three arithmetic statements above will give different answers.

 if(isWriting==true && isStanding==false || uNum==2){
 
 }

is different from

 if(isWriting==true && (isStanding==false || uNum==2)){
     
     }

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 Firedan1176 · Apr 14, 2014 at 09:09 PM

Try placing your ANDs and ORs in their own groups of if statements. I've had trouble by placing && || && and it doesn't work right. So try placing all your && in one, and your || in another.

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

26 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

Related Questions

Simple && and || question. 1 Answer

if get key and float bigger as 1 Answer

C# Reload Script 1 Answer

Separate script from reload? 0 Answers

And statement? 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