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 /
This question was closed Oct 15, 2016 at 08:30 PM by stephen_george98 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by stephen_george98 · Oct 08, 2016 at 11:15 PM · c#erroreventsystemvoidoperator

Error CS0023: The '!' operator cannot be applied to operand of type 'void'

Hello,

Here is part of my script:

 void Update () 
     {
         if (!GoRight () || !GoLeft()) 
         {
             CharacterMover.Instance.LeftRight = 0;
         }
     }
 
     public void GoRight ()
     {
         CharacterMover.Instance.LeftRight = 1;
     }
     public void GoLeft ()
     {
         CharacterMover.Instance.LeftRight = -1;
     }

My error is at line 3 inside the IF statement. The two methods "GoLeft" and "GoRight" are connected to a button via OnPointerDown on the Eventsystem. All I want is whenever none of those two functions are being called, the LeftRight float from another script goes to 0. Any ideas on how to fix this error? Thank you

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

1 Reply

  • Sort: 
avatar image
-1
Best Answer

Answer by JathOsh · Oct 09, 2016 at 02:45 AM

void means they do not return something if you were to do ! it would need to return true like:

  void Update () 
          {
              if (!GoRight () || !GoLeft()) 
              {
                  CharacterMover.Instance.LeftRight = 0;
              }
          }
      
          public bool GoRight ()
          {
              CharacterMover.Instance.LeftRight = 1;
              return true;
          }
          public bool GoLeft ()
          {
              CharacterMover.Instance.LeftRight = -1;
              return true;
          }

These will both always return true so you might want an if statement deciding weather or not they should return true or false

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 Bonfire-Boy · Oct 09, 2016 at 02:50 AM 0
Share

I've been struggling with how to answer this one. I'm afraid your answer isn't it and will only make things worse.

Your code fixes the compilation problem, but it won't achieve what the OP is trying to do, nor will it help with their lack of understanding of the concepts of functions, variables, and program flow.

Note that, as they have said, the GoLeft() and GoRight() functions are called by button events. They should not be calling those functions explictly in the Update() function at all.

What they really need to do is go back to basics and learn some program$$anonymous$$g fundamentals.

avatar image Bonfire-Boy · Oct 09, 2016 at 02:59 AM 0
Share

The simplest way to do it might be just to replace the whole Update() function with a LateUpdate() that sets the LeftRight variable to zero. That way it will start every frame as zero and only be changed to something else in frames where one of those buttons sends a "down" event.

Another, probably better, solution would be to add a function to hook up to the equivalent PointerUp() events, and have that function set the LeftRight variable back to zero.

It depends on what they're trying to achieve. But the Update() function definitely needs to be removed, as opposed to mangling the rest of the code in order to allow the Update function to compile.

Follow this Question

Answers Answers and Comments

237 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Weird Error c# 1 Answer

Please Help 1 Answer

Multiple Cars not working 1 Answer

C# Error help ( error CS0023 The `!’ operator cannot be applied to operand of type `string’ ) ??? 1 Answer

Distribute terrain in zones 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