Issues with c# script, minor error that I am unsure how to fix
Hi I am having issues with the following piece of code:
{
 public float minTarX = 1;
 public float maxTarX = 10;
 public float minTarZ = 1;
 public float maxTarZ = 10;
 public float tarX;
 public float tarZ;
 
 public float dampX;
 public float dampZ;
 int timeSwitch = 100;
 public GameObject target;
 
 void  Start ()
 {
     CreateTarPoint();
 }
 
 void  Update ()
 {
     if(target != null)
     {
         transform.GetComponent<"NavMeshAgent">().destination = target.position;
     }
     else
     {
         if(timeSwitch <= 0)
         {
             timeSwitch = 100;
             CreateTarPoint();
         }
         else
         {
             transform.GetComponent<"NavMeshAgent">().destination = new Vector3(tarX, 0, tarZ);
             timeSwitch -=1 * Time.deltaTime;
         }
     }
 }
 
 void  CreateTarPoint ()
 {
     dampX = Random.Range(1.0f, 3.0f);
     dampZ = Random.Range(1.0f, 3.0f);
     
     tarX = Random.Range(minTarX, maxTarX) - dampX;
     tarZ = Random.Range(minTarZ, maxTarZ) - dampZ;
 }
 
 void  OnTriggerEnter ( Collider col  )
 {
     if(col.gameObject.tag == "Player")
     {
         target = col.transform;
     }
 }
 
 void  OnTriggerExit ( Collider col  )
 {
     if(col.gameObject.tag == "Player")
     {
         target = null;
     }
 }
 
 
               }
Specifically I am having issues here:
void Update () { if(target != null) { transform.GetComponent<"NavMeshAgent">().destination = target.position; } }
Where is says: <"NavMeshAgent">().destination = target.position; I have a red error with the () parenthesis, I remove them and then I have another error with the full stop, I remove that and then I receive another error with the = symbol. I used a conversion system to bring this code to c# from Javascript. Would someone please be able to assist me? Thanks in advance.
Answer by Kielgasten · Jan 06, 2016 at 09:49 PM
Hi,
There might be other things, but start with removing the "" around <"NavMeshAgent">
This is to say the lines should look like this:
  transform.GetComponent<NavMeshAgent>().destination = new Vector3(tarX, 0, tarZ);
 
               As an aside, consider caching your components in the Start(), that way you won´t spend resources finding the NavMeshAgent component every frame.
Hi,
Thank you for your help, it fixed the issue I was having but the issue I am now having is this:
Error CS0266: Cannot implicitly convert type float' to int'. An explicit conversion exists (are you missing a cast?)
This error is in relation to this line:
timeSwitch -=1 * Time.deltaTime;
You wouldn't happen to have any advice on this would you?
in the timeswitch declaration (at line 10) change int to float. C# enforces type safety. This means that once a variable has been declared (here an int) only int values can be assigned to it. On that particular line you are attempting to assign a float value to it.
Javascript does not do this, which is why it probably worked before.
Thank you very much for your help. I think I have it sorted now, although the job of this script is to make an enemy move randomly, and the enemy isn't moving with the script attached :P but thank you anyway. If you were willing to help more this issue seems to be stem$$anonymous$$g from this:
public GameObject target;
In the tutorial I used (which was made in Java) the gentleman wrote var target : Transform on that line, which I figured changing it to be a public Transform might alleviate that issue but it seems to not be perfor$$anonymous$$g the originally intended function. The cube I have attached this script to won't move, the numbers are generated but the cube remains still, I have also baked a Navmesh onto a plane.
Your answer
 
             Follow this Question
Related Questions
how to create a matrix in C#? 1 Answer
Convert Java to C# 1 Answer
JavaScript to C# 1 Answer
How to change from C# to JAVA? 4 Answers
Error CS1525: Unexpected Symbol ')' on lines 13 and 14 2 Answers