Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Debug.DrawLine (Help!)

Discussion in 'Scripting' started by ActivePrime, May 14, 2016.

  1. ActivePrime

    ActivePrime

    Joined:
    Jan 28, 2014
    Posts:
    147
    Hello Unity,

    I am trying to draw line between my character and the enemy, everything seems to be fine on my end but it doesn't work. I have either no errors or warnings and my character is tagged "Player".
    I am sharing the code, please do let me know what's wrong.

    Regards,
    M.Aqib

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class EnemyAI : MonoBehaviour
    5. {
    6.  
    7.     public Transform target;
    8.  
    9.     public GameObject playerFinder;
    10.     private Transform myTransform;
    11.  
    12.  
    13.  
    14.     void Awake()
    15.     {
    16.         myTransform = transform;
    17.     }
    18.  
    19.     void Start()
    20.     {
    21.         playerFinder = GameObject.FindGameObjectWithTag("Player");
    22.         target = playerFinder.transform;
    23.     }
    24.  
    25.     void Update()
    26.     {
    27.  
    28.         Debug.DrawLine(target.position, myTransform.position, Color.yellow);
    29.    
    30.     }
    31. }
     

    Attached Files:

  2. Kona

    Kona

    Joined:
    Oct 13, 2010
    Posts:
    208
    Sounds odd! I suppose it should not do any difference since you're calling drawLine from the Update function, but you could try adding the duration the line should be seen.

    Code (CSharp):
    1. Debug.DrawLine( target.position, myTransform.position, Color.yellow, 1f );
    But as far as I can see there's nothing wrong with your code, you could also try to create an OnDrawGizmos member and use Gizmos.DrawLine instead?

    Code (CSharp):
    1. public void OnDrawGizmos(){
    2. if( target != null ){
    3. Gizmos.DrawLine( target.position, myTransform.position, Color.yellow );
    4. }
    5. }
     
    ActivePrime likes this.
  3. ericbegue

    ericbegue

    Joined:
    May 31, 2013
    Posts:
    1,353
    Debug.Draw is not enable by default in the game window, you need to enable the draw gizmo option. Though it should be always visible in the editor window.
     
    ActivePrime likes this.
  4. JoshuaMcKenzie

    JoshuaMcKenzie

    Joined:
    Jun 20, 2015
    Posts:
    897
    if there is no error, then its finding the player and drawing the line, otherwise you would've gotten a Null reference exception on target.position.

    Just like the Handles class, I honestly think it makes more sense to me that the Debug class should only load into a build if "Developmental build" is checked, by being in UnityEditor assembly to help enforce the idea that it shouldn't run during a released build. Same with Gizmos class which normally shows up only in scene view (unless you have that "show gizmos" on, which again only works in editor). they are considered Editor utilities, not classes to be used in the released game.

    If you want to see this line in the game during a build then don't use the Debug or gizmos classes. Use a Line Renderer
     
    ActivePrime likes this.
  5. ActivePrime

    ActivePrime

    Joined:
    Jan 28, 2014
    Posts:
    147
    Code (CSharp):
    1. Debug.DrawLine( target.position, myTransform.position, Color.yellow, 1f );
    The code worked properly, what is "1f" you have added in the code ?

    Thanks for sharing clarified code, it helped me learn :D
     
  6. ActivePrime

    ActivePrime

    Joined:
    Jan 28, 2014
    Posts:
    147
    All of you, thanks for helping!
    Cheers :p
     
  7. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148

    http://docs.unity3d.com/ScriptReference/Debug.DrawLine.html

    fourth parameter is "duration", defaults to 0f.