Search Unity

[SOLVED] Line renderer lightning works when playing in editor, but not through build and run

Discussion in 'Scripting' started by xolotlll, Jul 17, 2017.

  1. xolotlll

    xolotlll

    Joined:
    Jul 16, 2017
    Posts:
    18
    Hi all,

    I've made a lightning effect that I'm quite happy with using line renderers. It works perfectly when I click the play button at the top of the editor window. Here's a quick screenshot:



    Unfortunately when I build and run, the lines don't show up at all. Every other indicator that the lightning strikes are happening does, though, including the printed message 'ZAP', and the intermittent point lights that appear. Here's what it looks like using build and run:



    For whatever reason, the lines are visible as they should be when I play through the editor, and invisible when I build and run.

    Does anybody know how I can get the lines to show up?

    I've tried playing with the sort order of the line renderer, and the depth and clipping planes of the camera, but none of that works.

    Thanks!

    Here's my code for reference:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Electricity : MonoBehaviour {
    6.  
    7.     private Color myColor = new Vector4(1, 1, 1, 1);
    8.     private Vector3 linePos;
    9.     private Vector3 lineEnd;
    10.     private Vector3 linePos2;
    11.     private Vector3 lineEnd2;
    12.     private int lines = 0;
    13.     private float rand;
    14.     public int numSegments;
    15.  
    16.     void Update()
    17.     {
    18.         if (Random.Range(0, 1000) <= 200f) //chance of lightning normally just 5
    19.         {
    20.  
    21.             StartCoroutine(strike());
    22.  
    23.         }
    24.    
    25.     }
    26.  
    27.     void DrawLine(Vector3 start, Vector3 end, Color color, float duration = 0.2f)
    28.     {
    29.         GameObject myLine = new GameObject();
    30.         myLine.transform.position = start;
    31.  
    32.         myLine.AddComponent<Light>();
    33.         Light l = myLine.GetComponent<Light>();
    34.         l.color = myColor;
    35.         l.intensity = 2;
    36.         l.range = 6;
    37.  
    38.         myLine.AddComponent<LineRenderer>();
    39.         LineRenderer lr = myLine.GetComponent<LineRenderer>();
    40.         lr.material = new Material(Shader.Find("Particles/Alpha Blended Premultiply"));
    41.         lr.startColor = myColor;
    42.         lr.endColor = myColor;
    43.         lr.startWidth = .01f;
    44.         lr.endWidth = .01f;
    45.         lr.SetPosition(0, start);
    46.         lr.SetPosition(1, end);
    47.         Destroy(myLine, duration);
    48.     }
    49.  
    50.     void makeSegment()
    51.     {
    52.         linePos = lineEnd;
    53.         lineEnd = new Vector3(linePos.x + Random.Range(0f, 0.16f) - Random.Range(0f, 0.16f), linePos.y - Random.Range(.02f, 0.32f), linePos.z + Random.Range(0f, 0.16f) - Random.Range(0f, 0.16f));
    54.         DrawLine(linePos, lineEnd, myColor, .1f);
    55.  
    56.         if (lines > 0)
    57.         {
    58.             linePos2 = lineEnd2;
    59.             lineEnd2 = new Vector3(linePos2.x + Random.Range(0f, 0.16f) - Random.Range(0f, 0.16f), linePos2.y - Random.Range(.02f, 0.32f), linePos2.z + Random.Range(0f, 0.16f) - Random.Range(0f, 0.16f));
    60.             DrawLine(linePos2, lineEnd2, myColor, .1f);
    61.         }
    62.     }
    63.  
    64.     IEnumerator strike()
    65.     {
    66.        
    67.         //first segment
    68.         lineEnd = new Vector3(transform.position.x + Random.Range(.08f, 3.2f) - Random.Range(.08f, 3.2f), transform.position.y, transform.position.z + Random.Range(.08f, 3.2f) - Random.Range(.08f, 3.2f));
    69.         print("ZAP");
    70.  
    71.             for(int i = 0; i < numSegments; i++)
    72.             {
    73.  
    74.                 makeSegment();
    75.  
    76.                 if (Random.Range(0, 100) <= 25f) //25% chance of branching off with another line
    77.                 {
    78.                     lines += 1;
    79.                     //linePos = lineEnd;
    80.                     lineEnd2 = new Vector3(linePos.x + Random.Range(0f, 0.16f) - Random.Range(0f, 0.16f), linePos.y - Random.Range(.02f, 0.32f), linePos.z + Random.Range(0f, 0.16f) - Random.Range(0f, 0.16f));
    81.                     DrawLine(linePos, lineEnd2, myColor, .1f);
    82.                 }
    83.    
    84.                 yield return new WaitForSeconds(0.01f);
    85.  
    86.             }
    87.  
    88.     }
    89.  
    90.  
    91. }
    92.  
     
  2. xolotlll

    xolotlll

    Joined:
    Jul 16, 2017
    Posts:
    18
    Solved: I needed to go to Edit > Project Settings > Graphics, and include the shader I'm using under 'Always Included Shaders'.
     
    BakeMyCake likes this.
  3. BakeMyCake

    BakeMyCake

    Joined:
    May 8, 2017
    Posts:
    175
    I gave your script a whirl and came to the same conclusion before you posted your reply. Glad you have solved it, but the whole thing made me quite curious about your project. Is your project represented online somwhere? I'm really curious to see what it is.
     
  4. xolotlll

    xolotlll

    Joined:
    Jul 16, 2017
    Posts:
    18
    Sure! Will PM you a link : )
     
  5. FantasticGlass

    FantasticGlass

    Joined:
    May 31, 2016
    Posts:
    38
    This thread was a huge help, solved a problem I've been having for a week now.
    Just wanted to say thanks!