Cannot convert type unityEngine.Vector3 to float line 25

Simple script i do not think i have to explain i am newbie anyways.

public class test : MonoBehaviour {
   
    public bool somebool;



    void Start () {
        somebool = false;

    }
   

    void Update () {

        if (somebool) {
             moveCube(3f);
        }
    }

    float moveCube(float x)
    {
        return transform.position += new Vector3 (x,0,0);
    }

}

Line 20 you define the function as a float
line 22 you attempt to return a vector3

suggestion: make line 20 ā€œvoid MoveCubeā€¦ā€ then it doesnā€™t need to return anything (so delete ā€˜returnā€™)

1 Like

Thanks i am still learning i forgot Vector3 had its own type.

haha i took it to a complete new level and did this

public class test : MonoBehaviour {
   
    public bool somebool;



    void Start () {
        somebool = false;

    }
   

    void Update () {

        if (somebool) {
             transform.position+=new Vector3(moveCube(3f),0,0);
        }
    }

    float moveCube(float x)
    {
        Vector3 vec = new Vector3 ();
        vec.x = x;
        return x;

    }

}

where i could have just done this

public class test : MonoBehaviour {
   
    public bool somebool;



    void Start () {
        somebool = false;

    }
   

    void Update () {

        if (somebool) {
             transform.position+=new Vector3(moveCube(3f),0,0);
        }
    }

    float moveCube(float x)
    {
        return x;

    }

}

Why did you bother to make a method that returns the same thing you put into it? Just define a variable.

public float xMove = 3.0f;

Or this

transform.position += Vector3.right * 3;

Using methods is all well and good, but the methods should do something worth doing. Its also worth noting that using a method for something that takes only a single line of code can be a bad idea, and can often make your code more difficult to read.

1 Like

Thatā€™s also a suitable and more elegant solution. You should never use a method to return the same as youā€™ve put in. Itā€™s sloppy and definitely not good coding practice. Iā€™d fire someone for that :-p

Occasionally itā€™s worth doing. Say itā€™s meant to be a big complex method, but you donā€™t intend to develop it till later. It can also be done to build a dummy interface implementation for testing purposes. I can also see something like a buff system in an RPG, that will sometimes retun a value unmodified.

So donā€™t get to quick with the warning notices. :slight_smile:

But on the OP script Iā€™m pretty confident itā€™s pointless. There are also extraneous assignments that are totally unneeded.

If they were going to flesh out the method later to return a modified value then it would be acceptable. I would certainly ask why they used a method instead of a variable. If they have proper justification I would have no problem with it, but if you are using a method to return a constant value that is never modified its just plain wrong. Thereā€™s no reason to obfuscate something thatā€™s already as complex as writing software. I try to code for ease of use and simplicity. This is pretty important when you are working as part of a team in an agile scrum environment.

1 Like

Chill, this is just for coding practice i mentioned i am a newbie at this i completely understand that there is no use for this method if its only going to represent 1 statement.

If it is for practice you should practice good standards. You can use a method, but make it a method that returns a modified value. Thatā€™s much better practice. Not trying to offend you here; rather just making observations. Best of luck to you and try not to get discouraged :slight_smile:

Noob questions often lead to more complex discussions once the question is answered. Itā€™s how we keep ourselves same when every other question is a missing semi colon. Forget reading it as a comment on your work, read it as a discussion between two coders on the merits of a particular technique.

1 Like

Well i am reading a book(686 pages) called c# programming with unity3D thatā€™s all.