- Home /
Error with Camera WorldtoScreenPoint
Problem trying to convert a JS script to C#. The below JS has been converted to C#
JS
static function check_res(g:GameObject):float
{
var map:_mapObject = g.GetComponent("_mapObject");
var w2s = Camera().main.WorldToScreenPoint;
var m:MeshFilter = g.GetComponent(MeshFilter);
var mv:Vector3[] = m.mesh.vertices;
var t = g.transform.position;
var d = Vector3.Distance(w2s(mv[0]+t), w2s(mv[8]+t));
d += Vector3.Distance(w2s(mv[2]+t), w2s(mv[6]+t));
return d;
}
C#
public static float check_res(GameObject g)
{
_mapObject map= (_mapObject) g.GetComponent("_mapObject");
Camera w2s = new Camera().main.WorldToScreenPoint;
MeshFilter m= (MeshFilter)g.GetComponent(typeof(MeshFilter));
Vector3[] mv= m.mesh.vertices;
var t = g.transform.position;
var d = Vector3.Distance(w2s(mv[0]+t), w2s(mv[8]+t));
d += Vector3.Distance(w2s(mv[2]+t), w2s(mv[6]+t));
return d;
}
The issue is Static member
UnityEngine.Camera.main' cannot be accessed with an instance reference, qualify it with a type name instead`
You do not need to create a new instance of a Camera. main on Camera is static and returns the Camera named "$$anonymous$$ainCamera" you do not need to create an instance(the new keyword in c#) since it's static. However, i'm not sure how the other JS version worked. WorldToScreenPoint take a Vector3 Parameter, you know the point in the world to return as a point on the screen.
WorldToScreenPoint Documentation
So, your question is incomplete and is missing detail.
Answer by devluz · Oct 09, 2014 at 05:02 AM
I am partly guessing but is that what you want?
public static float check_res(GameObject g)
{
MeshFilter m = (MeshFilter)g.GetComponent(typeof(MeshFilter));
Vector3[] mv = m.mesh.vertices;
var t = g.transform.position;
var d = Vector3.Distance(Camera.main.WorldToScreenPoint(mv[0] + t), Camera.main.WorldToScreenPoint(mv[8] + t));
d += Vector3.Distance(Camera.main.WorldToScreenPoint(mv[2] + t), Camera.main.WorldToScreenPoint(mv[6] + t));
return d;
}
Your answer
Follow this Question
Related Questions
Using the same controller button twice 2 Answers
WorldToViewportPoint and WorldToScreenPoint give wrong positions when VR is enabled 1 Answer
Screen position out of view frustum error on FOV change? 0 Answers
Make camera follow an instantiated object 1 Answer
How can I accurately convert a Game Object's position to a sub Canvas/Rect screen position? 0 Answers