Problem with script conversion from JS to C#

Hello:)

Im begginer so dont be angry Guys andGirls :slight_smile:

Im trying to convert this JS script to C# but I have some problems…

var follow :GameObject;

function LateUpdate ()
{
	var cam = GetComponent("Camera");
	if(cam != null)
	{
		if(follow)
		{
			cam.transform.position = Vector3(follow.transform.position.x, cam.transform.position.y, follow.transform.position.z);
		}
	}
}

I was trying to use this site - Convert unity javascript (unityscript) to C#
I got something like this:

using UnityEngine;
using System.Collections;

public class MYCLASSNAME : MonoBehaviour {
GameObject follow;

void  LateUpdate (){
	FIXME_VAR_TYPE cam= GetComponent<"Camera">();
	if(cam != null)
	{
		if(follow)
		{
			cam.transform.position = Vector3(follow.transform.position.x, cam.transform.position.y, follow.transform.position.z);
		}
	}
}
}

And this is not working unfortunately :/.
So im Stuck, because this script in C# doesnt work :/… Any idea? I thought that cam should be bool type but this is not that simple :P. When I set bool cam = Get… I got error error CS1525: Unexpected symbol `)’ - this error was pointing at “bool cam = GetComponent<“Camera”>();”

Help :E

Quick question - why are you trying to convert it to C# ?

Because I decided to use C# in my games(I knew some basics from C/C++). And I think that it could be handful If I wanna use it in near future with XNA for example.

And C# is C#. I know that using JS could be simpler becaue of many things but C# is my choice :P. Is bad decision :(?

Bad Luck, that many tutorials comes with JS not C#, so I decided to try convert them to C#.

The GetComponent you are using there requires a type, not a string. Removing the inverted commas inside GetComponent<“Camera”>() should fix it.

If you absolutely need it as a string, you’ll need to write it as GetComponent(“Camera”) as Camera

So it should looks like:

cam = GetComponent();
?
But I think that not - get some errors about “The name `cam’ does not exist in the current context” pointing at :
“cam.transform.position = Vector3(follow.transform.position.x, cam.transform.position.y, follow.transform.position.z);”

Could anyone help my get properly working script :)?

FIXME_VAR_TYPE cam= GetComponent<"Camera">();

Should be

Camera cam = GetComponent<Camera>();

It did say FIXME =P

Though I prefer to declare cam outside of the function and just pass in the component in Start/Awake so you don’t have to call GetComponent repeatedly.

EDIT
Also, if you want to check if the game object follow is null, you can’t do if(follow). You need to write it as if(follow != null).

Thx wolfhunter777!

Now I have only 1 error: “Expression denotes a type', where a variable’, value' or method group’ was expected” and this error is pointing at “cam.transform.position = Vector3(follow.transform.position.x, cam.transform.position.y, follow.transform.position.z);”

Ohh I figured it out :F.

I forget about “new” before “Vector3(…)”. Im right:)?

yes :slight_smile:

The ‘new’ statement will fool you for a while, but than becomes natural to understand when/where to use it.

You could use it if the script is attached to the game object you want to find;

Camera cam = gameObject.GetComponent("Camera") as Camera;

Or just use the fast way;

Camera cam = Camera.mainCamera;