Error in C# NoError in JS .. !!

… For move smoothly to object position, the same code worked well in JS, but gave me error C# …:

Javascript Code:

var target : Transform;

function Update ()
{
transform.position = Vector3.Lerp(this.transform.position, Vector3(target.transform.position.x,target.transform.position.y,target.transform.position.z), 0.2);
}

C# Code:

public Transform target;

void Update ()
{
transform.position = new Vector3.Lerp(this.transform.position, new Vector3(target.transform.position.x,target.transform.position.y,target.transform.position.z), 0.2);
}

Console C# Errors:

 error CS0426: The nested type `Lerp' does not exist in the type `UnityEngine.Vector3'

What is the reason of this problem ?

remove the new keyword. It is only for creating new instances! Vector3.Lerp() is a static method, so no need for it

No it didn’t, the code isn’t the same.

element nailed it.

ok…, i removed new keyword from Vector3.Lerp method, there are more errors:

The Code:

using UnityEngine;
using System.Collections;

public class NewBehaviourScript1 : MonoBehaviour {
	public Transform target;
	
	// Update is called once per frame
	void Update () {
	transform.position = Vector3.Lerp(this.transform.position, new Vector3(target.transform.position.x,target.transform.position.y,target.transform.position.z), 0.2);

	}
}

The Console:

error CS1502: The best overloaded method match for `UnityEngine.Vector3.Lerp(UnityEngine.Vector3, UnityEngine.Vector3, float)' has some invalid arguments
error CS1503: Argument `#3' cannot convert `double' expression to type `float'

well, you see that error message, right?! can not convert double to float. So now you will go to this link, read the page. http://msdn.microsoft.com/en-us/library/b1e65aza(v=vs.90).aspx

EDIT: changed Link to english documentation… my bad :wink:

(0.2).GetType() = double
(0.2F).GetType() = float
Just add “F”

Why aren’t you just using target.transform.position as the second argument?