Error CS0019: Operator '+' cannot be appied to operands of type 'Vector3' and 'float'

Hi Guys,

I use Unity 2018.3.4f1, SteamVR: 2.2.0 and I have a HTC Vive. I’m struggling, because there are so many outdated tutorials and nothing that would work for my setup. Anyway, I think this tutorial could actually help me regarding interacting with the Canvas:

Unfortunately I get an error and I don’t know how to fix it or work around. I really could use some help here :wink: - do you have any suggestions?

This ist my code (detected error in red):

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Pointer : MonoBehaviour
{
public float m_DefaultLength = 5.0f;
public GameObject m_Dot;
public LaraVRImportModule m_InputModule;

private LineRenderer m_LineRenderer = null;

private void Awake()
{
m_LineRenderer = GetComponent();
}

private void Update()
{
UpdateLine();
}

private void UpdateLine()
{
// Use default or distance
float targetLength = m_DefaultLength;

// Raycast
RaycastHit hit = CreateRaycast(targetLength);

// Default End (if we don’t hit anything where is the dot?!)
Vector3 endPosition = transform.position + (transform.forward + targetLength);

//Or based on hit
if (hit.collider != null)
endPosition = hit.point;

// Set position of the dot
m_Dot.transform.position = endPosition;

// Set linerenderer
m_LineRenderer.SetPosition(0, transform.position);
m_LineRenderer.SetPosition(1, endPosition); //second position

}

private RaycastHit CreateRaycast(float length)
{
RaycastHit hit;
Ray ray = new Ray(transform.position, transform.forward);
Physics.Raycast(ray, out hit, m_DefaultLength);

return hit;
}
}

adding a scalar number to vector doesn’t make sense mathematically.
Are you sure that

(transform.forward + targetLength);

isn’t supposed to be
(transform.forward * targetLength);?

Mathematically you can either multiply a vector by a single value or you can add a single value to one of the three values in the vector.

So its either something like someVector * someFloatValue or someVector.x + someFloatValue.

Vector3 newVec = Vector3.up;
// results in (0,1,0)

float multiplier = 5.0f;

newVec *= multiplier;
// results in (0,5,0)

newVec.x += 3.0f;
// results in (3,5,0)
1 Like

hm, i’m sure that they used a plus within the tutorial…
I think the multiplyer operator would result in a relative distance (which would not always be the same) since the transform ist not always the same, wouldn’t it?

Maybe you are confusing transform.forward with transform.position?

THe magnitude of transform.forward is always 1. When you multiply targetLength, the result will be that you are moving the position by targetLength units in the object’s forward direction.

1 Like

oh, that totally solves more than just one problem!!! got that transform.forward totally wrong, I thought the 1 was just used as example in all the descriptions :sweat_smile:
THAAANX!!!

And most people who came to Unity in the old days knew about vector+float. They were using it inside shaders, where UV+=0.1 (adding 0.1 to the both) is legal and useful.

But I think removing it was a good call on Unity’s part, exactly because the OP got an error. If they’d left it legal, this guy would have a weird, hard to debug, program moving diagonally and a little more forward. It’s almost never needed. I sometimes want to use someColor+=0.1 to make it a little lighter, but you pretty much never need position+=1 or scale+=1 or rotation+=1. And someColor+=Color.White*0.1 won’t kill me.

1 Like