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

Navmesh Linerenderer - Javascript to C# Conversion

Discussion in 'Scripting' started by tcmeric, Apr 28, 2017.

  1. tcmeric

    tcmeric

    Joined:
    Dec 21, 2016
    Posts:
    190
    I converted (and had someone else check over) a script from Javascript to C#. The original javascript code does what it is supposed to, however the C# script does not. It does not throw any errors however.

    The script draws a line between an navmesh agent and the destination, using a line renderer component. The javascript works correctly, but the C# script when plays, the value for the navmesh agent disappears from the inspector (and no line is rendered).

    I created a game object and attached a line renderer. In the script I assign the line renderer component, a game object with a nav mesh agent (in this case a cube), bake the nav mesh per normal, and then a destination object.

    Here is the javascript:



    Code (JavaScript):
    1. var line : LineRenderer; //to hold the line Renderer
    2. var target : Transform; //to hold the transform of the target
    3. var agent : NavMeshAgent; //to hold the agent of this gameObject
    4.  
    5. function Start(){
    6.     line = GetComponent(LineRenderer); //get the line renderer
    7.     agent = GetComponent(NavMeshAgent); //get the agent
    8.     getPath();
    9. }
    10.  
    11. function getPath(){
    12.     line.SetPosition(0, transform.position); //set the line's origin
    13.  
    14.     agent.SetDestination(target.position); //create the path
    15.     yield WaitForEndOfFrame(); //wait for the path to generate
    16.  
    17.     DrawPath(agent.path);
    18.  
    19.     agent.Stop();//add this if you don't want to move the agent
    20. }
    21.  
    22. function DrawPath(path : NavMeshPath){
    23.     if(path.corners.Length < 2) //if the path has 1 or no corners, there is no need
    24.         return;
    25.  
    26.     line.SetVertexCount(path.corners.Length); //set the array of positions to the amount of corners
    27.  
    28.     for(var i = 1; i < path.corners.Length; i++){
    29.         line.SetPosition(i, path.corners[i]); //go through each corner and set that to the line renderer's position
    30.     }
    31. }
    Here is the C#

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.AI;
    5.  
    6. public class drawNavMesh : MonoBehaviour {
    7.  
    8.     /*....................................*/
    9.     /*.............Variables..............*/
    10.     /*....................................*/
    11.  
    12.     /*...........LineRenderer.............*/
    13.     public LineRenderer line;
    14.  
    15.     /*.........Target Transform...........*/
    16.     public Transform target;
    17.  
    18.     /*...........NavMeshAgent.............*/
    19.     public NavMeshAgent agent;
    20.  
    21.  
    22.     /*....................................*/
    23.     /*.............Functions..............*/
    24.     /*....................................*/
    25.  
    26.  
    27.  
    28.     // Use this for initialization
    29.     void Start () {
    30.  
    31.         // Get the line renderer...
    32.         line = GetComponent<LineRenderer>();
    33.  
    34.         // Get the agent...
    35.         agent = GetComponent<NavMeshAgent>();
    36.  
    37.         //Get Path...
    38.         getPath();
    39.     }
    40.  
    41.     IEnumerator getPath() {
    42.  
    43.         // Set the line's origin...
    44.         line.SetPosition (0, transform.position);
    45.  
    46.         // Create the path...
    47.         agent.SetDestination(target.position);
    48.  
    49.         // Wait for the path to generate...
    50.         yield return new WaitForEndOfFrame();
    51.  
    52.  
    53.         // Draw Path...
    54.         DrawPath(agent.path);
    55.  
    56.         // Add this if you don't want to move the agent...
    57.         agent.Stop();
    58.     }
    59.  
    60.     void DrawPath(NavMeshPath path) {
    61.  
    62.         // If the path has 1 or no corners, there is no need...
    63.         if (path.corners.Length < 2) {
    64.  
    65.             return;
    66.         }
    67.  
    68.         // Set the array of positions to the amount of corners...
    69.         line.numPositions = path.corners.Length;
    70.         for (int i = 1; i < path.corners.Length; i++) {
    71.  
    72.             // Go through each corner and set that to the line renderer's position...
    73.             line.SetPosition(i, path.corners);
    74.         }
    75.     }
    76.  
    77.     // Update is called once per frame
    78.     void Update () {
    79.    
    80.     }
    81. }
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,773
    The script overwrites the values of both the LineRenderer and the NavMeshAgent in Start(). If you're setting those manually by drag-and-drop, then you'll want to remove those lines from Start(), or at least null-check them:
    Code (csharp):
    1.  
    2. if (agent == null) agent = GetComponent<NavMeshAgent>();
    3.  
     
  3. tcmeric

    tcmeric

    Joined:
    Dec 21, 2016
    Posts:
    190
    Thank you. I appreciate your assistance. I will give that a try!
     
  4. tcmeric

    tcmeric

    Joined:
    Dec 21, 2016
    Posts:
    190
    That stops the agent from clearing from the inspector, but the script still is not working. Still no console errors.
     
  5. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,773
    One issue I see is on line 73, you're missing an [ i ]. It's suspicious that you're not seeing any errors in the console with that code, because that absolutely should be generating one - SetPosition can't take an array. I just checked and I get a compiler error there.
     
  6. tcmeric

    tcmeric

    Joined:
    Dec 21, 2016
    Posts:
    190
    Oh sorry, I did add that in as well. I did have that error. No other changes however, except your suggestion.

    Code (CSharp):
    1.  
    2. // Go through each corner and set that to the line renderer's position...
    3. line.SetPosition(i, path.corners[i]);
    4.