Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

i need help to convert thisfrom Js to Cs

Discussion in 'Scripting' started by dr-mad, Mar 19, 2010.

  1. dr-mad

    dr-mad

    Joined:
    Feb 15, 2010
    Posts:
    19
    hi i have a question how do i convert this from Js to Cs

    i know javascript a bit and im trying hard to learn it but can anyone please convert these lines to Cs
    im a total beginer at this type please help me ;)


    //code

    var target : Transform;

    function Awake ()
    {
    ChangeTarget ();
    }


    function ChangeTarget ()
    {
    if (target)
    {
    controller = target.GetComponent(CharacterController);
    }
    }

    function SetTarget (t : Transform)
    {
    target = t;
    ChangeTarget();
    }

    //end code

    greets dr-mad
     
  2. matthewminer

    matthewminer

    Joined:
    Aug 29, 2005
    Posts:
    331
    Change the function keyword to void. This is the return type of the function. Since your functions only execute statements without returning anything, nothing (or void) is returned.

    Declaring variables has just slightly different syntax.

    Code (csharp):
    1. var target : Transform;
    becomes
    Code (csharp):
    1. Transform target;
    The var keyword is replaced by the variable's type, and it comes before the variable name.

    Now stick your modified code into a class. In Unity, create a new C# script and all that mumbo jumbo is there for you. Just remove the default functions in there and paste your own in.
     
  3. CaptainKiyaku

    CaptainKiyaku

    Joined:
    Feb 8, 2009
    Posts:
    324
    Should be like this:
    (keep in mind that you have to change "scriptName" to the name of the scriptfile you put this into it)

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class scriptName : MonoBehaviour
    6. {
    7.     Transform target;
    8.    
    9.     void Awake()
    10.     {
    11.         ChangeTarget();
    12.     }
    13.    
    14.    
    15.     void ChangeTarget()
    16.     {
    17.         if(target)
    18.         {
    19.             CharacterControler controller = target.GetComponent<CharacterController>();
    20.         }
    21.     }
    22.    
    23.     void SetTarget(Transform t)
    24.     {
    25.         target = t;
    26.         ChangeTarget();
    27.     }
    28. }
    29.  
     
  4. dr-mad

    dr-mad

    Joined:
    Feb 15, 2010
    Posts:
    19
    thank you guys very much :wink:

    it was good i now know how to start scripting in C#

    thanks very much
     
  5. Cyclops

    Cyclops

    Joined:
    Jan 31, 2010
    Posts:
    111