Search Unity

Lines shows in editor mode but not when game is running

Discussion in 'Scripting' started by cruising, Jun 24, 2015.

  1. cruising

    cruising

    Joined:
    Nov 22, 2013
    Posts:
    329
    Hello!

    Im drawing powerlines and the lines shows up in editor mode but not when game is running. Why?

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PowerLines : MonoBehaviour {
    5.  
    6.     public Material lineMat;
    7.  
    8.     public GameObject mainPoint;
    9.     public GameObject[] points;
    10.  
    11.     void DrawConnectingLines() {
    12.         if(mainPoint && points.Length > 0) {
    13.             // Loop through each point to connect to the mainPoint
    14.             foreach(GameObject point in points) {
    15.                 Vector3 mainPointPos = mainPoint.transform.position;
    16.                 Vector3 pointPos = point.transform.position;
    17.            
    18.                 GL.Begin(GL.LINES);
    19.                 lineMat.SetPass(0);
    20.                 GL.Color(new Color(lineMat.color.r, lineMat.color.g, lineMat.color.b, lineMat.color.a));
    21.                 GL.Vertex3(mainPointPos.x, mainPointPos.y, mainPointPos.z);
    22.                 GL.Vertex3(pointPos.x, pointPos.y, pointPos.z);
    23.                 GL.End();
    24.             }
    25.         }
    26.     }
    27.  
    28.     // To show the lines in the game window when it is running
    29.     void OnPostRender() {
    30.         DrawConnectingLines();
    31.     }
    32.  
    33.     // To show the lines in the editor
    34.     void OnDrawGizmos() {
    35.         DrawConnectingLines();
    36.     }
    37. }
     
    Last edited: Jun 24, 2015
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Is this script attached to a camera?
     
  3. cruising

    cruising

    Joined:
    Nov 22, 2013
    Posts:
    329
    No, it is attached to the object that the lines will be drawn from. Should it be placed on the camera?
     
  4. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Yes, as far as I know the GL class only works on a camera or during a post-processing image effect.
     
  5. cruising

    cruising

    Joined:
    Nov 22, 2013
    Posts:
    329
    I see!

    Then this code aint usefull for me since i need 7 lines per object, and i cant put all that in a camera.
     
  6. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    of course you can, you just need to change the way you handle line drawing.

    also, assuming you are using Unity 5?
     
  7. cruising

    cruising

    Joined:
    Nov 22, 2013
    Posts:
    329
    How? as the code are like now i need 1 script per 7 lines, or it bugs out and drawing lines or something when moving the mouse in the editor.

    EDIT: what i do now is that i have 1 script on the object 1, it draws the lines to object 2 that have the same script that then draw lines to object 3 etc.

    And since i need it on the camare, i dont know how to do that.
     
  8. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    How? code.

    Back to my other question, are you using unity 5?
     
  9. cruising

    cruising

    Joined:
    Nov 22, 2013
    Posts:
    329
    I know, but i dont know the way you think the code should be. Sorry..i forgot to answear that question, yes i use Unity 5.
     
  10. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Just grab all the objects you need (GameObject.FindGameObjectsWithTag?) and draw them together. We'll do our best to help if you explain what part you're struggling with.
     
  11. cruising

    cruising

    Joined:
    Nov 22, 2013
    Posts:
    329
    Im sorry, i didnt understand what you mean there, grab the lines or the objects where the lines will be draw between in to just 1 script?
    And im a bit confused here lol.

    And the lines is beeing drawn from sphere to sphere (7), and they are a child of the power line. witch means if i have for example 40 power lines i will have 280 lines and objects(spheres) in the script.
    Seams like a time consuming scripting lol
     
  12. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    All the objects. Drawing it all from one place would actually be more efficient than having 40 different scripts have their OnPostRender() called.

    I whipped you up a quick example:


    Code:
    Code (csharp):
    1.  
    2. public class DrawPowerLines : MonoBehaviour
    3. {
    4.    public GameObject[] poles;
    5.  
    6.    protected Material lineMaterial;
    7.  
    8.    protected void Start()
    9.    {
    10.      poles = GameObject.FindGameObjectsWithTag("Pole");
    11.    }
    12.  
    13.    protected void OnPostRender()
    14.    {
    15.      if(poles.Length < 2)
    16.        return;
    17.  
    18.      lineMaterial.SetPass(0);
    19.  
    20.      GL.Begin(GL.LINES);
    21.      GL.Color(Color.black);
    22.  
    23.      for(int i = 0; i < poles.Length - 1; ++i)
    24.      {
    25.        Transform current = poles[i].transform;
    26.        Transform next = poles[i + 1].transform;
    27.  
    28.        // "left" sphere
    29.        GL.Vertex3(current.position.x - 2, current.position.y + 6, current.position.z);
    30.        GL.Vertex3(next.position.x - 2, next.position.y + 6, next.position.z);
    31.  
    32.        // "right" sphere
    33.        GL.Vertex3(current.position.x + 2, current.position.y + 6, current.position.z);
    34.        GL.Vertex3(next.position.x + 2, next.position.y + 6, next.position.z);
    35.      }
    36.  
    37.      GL.End();
    38.    }
    39. }
    40.  
    To guarantee draw order, instead of using GameObject.FindGameObjectsWithTag(), you could also just manually add the game objects to the array via the editor (drag and drop).
     
  13. cruising

    cruising

    Joined:
    Nov 22, 2013
    Posts:
    329
    Your script works perfect. How ever i think my models offset (center) aint in the center of the model, so the lines position where it thinks the centre are are way off the actually pylon.

    I have to try the way you did on that picture. You said i could manually add the objects, but when i do, the script keep removing the object on start and mix the order they should be in, and also i had to make the material public since no material would be added.

    I have 1 more question, if the next pylon are -10 lower or higer will the lines still been drawn to the right positions? why i ask is because when i started the game, after the lines was on the last pylon, the line went down to the terrain.
     
  14. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    If you add all the poles manually, get rid of this line or it will overwrite what you've added through the editor:
    Code (csharp):
    1.  
    2. poles = GameObject.FindGameObjectsWithTag("Pole");
    3.  
    As for making sure the lines go to the correct positions, you'll have to play with the offsets I used:
    Code (csharp):
    1.  
    2.   // "left" sphere
    3.   GL.Vertex3(current.position.x - 2, current.position.y + 6, current.position.z);
    4.   GL.Vertex3(next.position.x - 2, next.position.y + 6, next.position.z);
    5.   // "right" sphere
    6.   GL.Vertex3(current.position.x + 2, current.position.y + 6, current.position.z);
    7.   GL.Vertex3(next.position.x + 2, next.position.y + 6, next.position.z);
    8.  
    Just change around the numbers to accurately match your models. It shouldn't matter if one is higher or lower than the next one, but it will matter if they're rotated as my code doesn't attempt to take that into account.