- Home /
Question by
FPSworrior · Jan 26, 2014 at 06:09 AM ·
translate
Need this translated. Js to C#.
Can anyone translate this I have no idea how to get this to work.
var target : Transform;
function Update()
{
GetComponent(NavMeshAgent).destination = target.position;
}
Comment
Best Answer
Answer by Invertex · Jan 26, 2014 at 07:29 AM
Some corrections from the other answer:
public Transform target;
void Update()
{
GetComponent<NavMeshAgent>().destination = target.position;
}
Since in his example script, using GetComponent itself is fine for him, then the component he is getting is likely already on the same object, so you only need to use GetComponent.
This script should be optimized better though, so you are not doing a GetComponent every frame...
Create a variable for the component as well, and set it in Start().
public Transform target;
private NavMeshAgent navMeshAgent;
void Start()
{
navMeshAgent = GetComponent<NavMeshAgent>();
}
void Update()
{
navMeshAgent.destination = target.position;
}
Answer by CostelloNicho · Jan 26, 2014 at 06:23 AM
public transform target;
void Update()
{
gameObject.GetComponent<NavMeshAgent>().destination = target.postion;
}