Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Error in C# NoError in JS .. !!

Discussion in 'Scripting' started by woodygoody, Sep 3, 2013.

  1. woodygoody

    woodygoody

    Joined:
    Aug 23, 2011
    Posts:
    164
    .. For move smoothly to object position, the same code worked well in JS, but gave me error C# ..:

    Javascript Code:
    Code (csharp):
    1. var target : Transform;
    2.  
    3. function Update ()
    4. {
    5. transform.position = Vector3.Lerp(this.transform.position, Vector3(target.transform.position.x,target.transform.position.y,target.transform.position.z), 0.2);
    6. }
    C# Code:
    Code (csharp):
    1. public Transform target;
    2.  
    3. void Update ()
    4. {
    5. transform.position = new Vector3.Lerp(this.transform.position, new Vector3(target.transform.position.x,target.transform.position.y,target.transform.position.z), 0.2);
    6. }
    Console C# Errors:
    Code (csharp):
    1.  error CS0426: The nested type `Lerp' does not exist in the type `UnityEngine.Vector3'
    What is the reason of this problem ?
     
  2. Deleted User

    Deleted User

    Guest

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

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,509
    No it didn't, the code isn't the same.

    element nailed it.
     
  4. woodygoody

    woodygoody

    Joined:
    Aug 23, 2011
    Posts:
    164
    ok.., i removed new keyword from Vector3.Lerp method, there are more errors:

    The Code:
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class NewBehaviourScript1 : MonoBehaviour {
    5.     public Transform target;
    6.    
    7.     // Update is called once per frame
    8.     void Update () {
    9.     transform.position = Vector3.Lerp(this.transform.position, new Vector3(target.transform.position.x,target.transform.position.y,target.transform.position.z), 0.2);
    10.  
    11.     }
    12. }
    The Console:
    Code (csharp):
    1. error CS1502: The best overloaded method match for `UnityEngine.Vector3.Lerp(UnityEngine.Vector3, UnityEngine.Vector3, float)' has some invalid arguments
    Code (csharp):
    1. error CS1503: Argument `#3' cannot convert `double' expression to type `float'
     
  5. Deleted User

    Deleted User

    Guest

    Last edited by a moderator: Sep 4, 2013
  6. AShim-3D

    AShim-3D

    Joined:
    Jul 13, 2012
    Posts:
    33
    (0.2).GetType() = double
    (0.2F).GetType() = float
    Just add "F"
     
  7. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Why aren't you just using target.transform.position as the second argument?