Operator '&&' cannot be applied to operands of type 'method group' and 'void'

Hey There,

I am new to scripting and i ran into a problem.
As i learned Java I learned i can ask somthing by if(a() && b());

now my problem:
player_controller: Screenshot by Lightshot
push_logic: Screenshot by Lightshot

if i want to test it unity says: Operator ‘&&’ cannot be applied to operands of type ‘method group’ and ‘void’

i dont get how to fix it?!

is someone there who can help me?

&& operator is a boolean operator.

Your method that your testing must return a boolean for the operator to work.

The error is suggesting that you’re attempting to operate it on a ‘method group’ (the name of a function, rather than the return… a function must be followed by parens to get its return), and a ‘void’. In the case of the ‘void’, that’s a method that you called, but it has no return value.

public void up()
{
//...
}

This method does not return anything, hence the ‘void’.

You can’t operate a boolean operator on method groups and voids…

Fixing it requires that the functions returns you’re comparing return true/false.

Also, when sharing code, it’s easier to just use code tags:

but what type of data am i using if i cant do it with “void”?

you want a boolean

http://prntscr.com/dvaf8s

i dont get it…

OK, you see this code right here:

if ((p.isUnder) && (p.up())
{
    this.up();
}

This is referring to both these methods:

public void up()
{
    transform.position += Vector3.up;
}

public bool isUnder()
{
    const float distanceUp = 1;
    return Physics.Raycast(transform.position, this.transform.up, distanceUp);
}

Problem is with how you’re calling them.

p.isUnder… no, it should be p.isUnder(). You need to CALL the funciton, not reference the function (method group).

And p.up()… it returns a void. Because that method is an action, it does something, not evaluates something.

Fixing the ‘up()’ half of your if statement requires you to explain why you’re attempt to compare that… it logically doesn’t follow.

Also, stop using screenshots… use code tags. That way we can copy/paste as well as ‘find’ stuff easily. Images are a pain in the butt to deal with.

I am Using it because i cant find it on the toolbar, sorry

I supplied a reference link to how to use code tags.

Ok, so i want to push up the box if I am under it on the y axis and im pushing the button to move my blob up.

so im standing under it and pushing it up … :slight_smile:

so i am checking IF i am under it AND the playiner pushing the UP Button

Well, what constitutes pushing up?

It’s not a function that increments the position in the up direction. It’s the code where you test if a key is down.

So something like:

if(p.isUnder() && Input.GetKeyDown(KeyCode.UpArrow))
{
    p.up();
}

Of course, since your existing ‘Update’ code is already testing for these keypresses, those are going to ALSO move it up. So you might want to remove that stuff as well.

using UnityEngine;
using System.Collections;

public class player_controller : MonoBehaviour
{
   // public Vector3 movement ==  
    // Use this for initialization
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.UpArrow))
        {
            transform.position += Vector3.up;
        }
        if (Input.GetKeyDown(KeyCode.DownArrow))
        {
            transform.position += Vector3.down;
        }
        if (Input.GetKeyDown(KeyCode.LeftArrow))
        {
            transform.position += Vector3.left;
        }
        if (Input.GetKeyDown(KeyCode.RightArrow))
        {
            transform.position += Vector3.right;
        }

    }
    public bool up()
    {
        transform.position += Vector3.up;
        return true;
    }

   public bool down()
    {
        transform.position += Vector3.down;
        return true;
    }
   public bool left()
    {
        transform.position += Vector3.left;
        return true;
    }
   public bool right()
    {
        transform.position += Vector3.right;
        return true;
    }



    public bool isUnder()
    {
        const float distanceUp = 1;
        return Physics.Raycast(transform.position, this.transform.up, distanceUp);
    }
}
using UnityEngine;
using System.Collections;

public class push_logic : MonoBehaviour
{
    public player_controller p;
    // Use this for initialization
    void Start()
    {
        p = GetComponent<player_controller>();

        if ((p.isUnder()) && Input.GetKeyDown(KeyCode.UpArrow))
        {
            p.up();
        }
    }

    // Update is called once per frame
    void Update()
    {
    }

}

the box isnt moving at all :frowning:

well, you’re testing if ‘isUnder’ and if the key is down during ‘Start’. That method only runs once.

You seem to have a bunch of code scattered all over the place… where did you get this code? What is important to keep?

Cause honestly, most of it can be completely stripped out.

I did it myself…
I think it all has to be in it but i dont know what I can stripp out.

do you mean i have to check it under Update()?

OK… lets forget all this code that you have for now. And try to rebuild from the ground up.

You say you want:

So from context I assume that “pushing the button to move” is pushing any of the arrow keys. And you want to move that relative to direction. So we need to be testing for each arrow key for a respective direction.

Furthermore, you only want this to happen if “I am under it”.

What does that mean?

Who is “i” and what is “it”.

You have this code:

Physics.Raycast(transform.position, this.transform.up, distanceUp);

But is there anything for the raycast to hit? Does the ‘it’ have a collider on it or something?

Furthermore, your code here:

p = GetComponent<player_controller>();

implies that the player_controller and push_logic scripts are both on the same object. Again where is the ‘it’? How are we pushing that? All your logic as it stands deals with what appears to be the ‘player’.

Is there anything else that should be going on as well? Is the ‘i’… the ‘player’ supposed to ALSO be able to move?

Ok
I’ll make a list how i want it to work:
The Player = The red ball
Block = brown thing that says “kiste” on it

The goal of the Game is to push boxes on to buttons to open a door to the next level.

The player should stand unter the Box and if he and the box have to go up by 1 Unit