Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by waterdog · Aug 27, 2013 at 10:39 AM · c#arraylinerenderer

Why does my array of LineRenders only render one line?

I don't quite get something. But, this is my first attempt at both Unity3D and C# so that doesn't really come as a surprise...;^) I have written other C# code that successfully uses a single LineRenderer to generate a line based on data read from an XML file. I want to generate several separate lines based on data from the XML file, where lines may have several segments, each with it's own LineRender. (Yes, I know there is something called Vectrosity out there...;^) (It doesn't appear to give me the kind of control I need over colors, I don't have a spare $30 at the moment, and I'd like to understand this anyway.))

I emulate the multiline input process here by just filling arrays with data. I've tried several variations on initializing the LineRenderers which I have left as comments in the code.

In the inspector I added an empty gameObject, attached a LineRenderer and my code. It runs without error, but, only shows one line segment in the game display. (I'm wondering if I need to add all the gameObjects and LineRenderers in the inspector and not rely on the script alone. I hope not!)

Also, it doesn't use the assigned colors for the segment it does render.

I'd really appreciate any input on what I'd doing wrong!

Sorry for not including the code formatted as code, but the Markdown formatting doesn't seem to work for me.

// Test of using array of LineRenderers in a script to generate segments of a
// line. Eventually to be incorporated into visualization data display. The
// array is two-dimensional. The first index counts the tracks, the second index
// counts the segments within a track. Doing the lines with individual 2 point
// LineRenderers allows changing the color in each segment for color mapping.
// Note the second index must be set to the largest number of track segments.

// NOTE: The hardcoded values used here will be determined by reading data from
// an XML file in the operational code. The segment endpoint colors will
// be determined by temperature color map. Endpoint positions will come
// from the data records.

using UnityEngine;
using System.Linq;

public class ArrayLineTest : MonoBehaviour {

// declare types public outside and set array sizes in Start() after read // data and get track and track record counts

 public LineRenderer[,] tracks; 
 public int trkcnt;
  public int[] segcnts;
 public Color[,,] segclrs;
 public int maxsegs;
  public Vector3[,,] segpos;
 
 void Start () {

     // Here would read data and obtain data set sizes:
     
       trkcnt = 3;  
             segcnts = new int[] {1,3,2}; // 3 tracks so must be 3 segment sets
     maxsegs = segcnts.Max();
     
     // Initialize LineRenderer array:
     
     tracks = new LineRenderer[trkcnt,maxsegs]; 
     
     // Here would determine start and end colors for segments based on 
     // temperature colormap (values here from CsharpColorNameTable.pdf/255)
     
         segclrs = new Color[trkcnt,maxsegs,2];   
     
     // track 0, with 1 segment:
     
     segclrs[0,0,0] = new Color(0f,0f,0f,1f);             // Black
     segclrs[0,0,1] = new Color(0f,1f,0f,1f);             // Lime

     // track 1, with 3 segments:
     
     segclrs[1,0,0] = new Color(0.729f,0.333f,0.824f,1f); // MediumOrchid
     segclrs[1,0,1] = new Color(1f,0.498f,0.314f,1f);     // Coral
     segclrs[1,1,0] = new Color(1f,0.498f,0.314f,1f);    
     segclrs[1,1,1] = new Color(0f,1f,1f,1f);             // Cyan
     segclrs[1,2,0] = new Color(0f,1f,1f,1f);
     segclrs[1,2,1] = new Color(1f,0f,0f,1f);             // Red
 
             // track 2, with 2 segments:
     
     segclrs[2,0,0] = new Color(0f,0f,0f,1f);             // Black
     segclrs[2,0,1] = new Color(0f,1f,0f,1f);             // Lime
     segclrs[2,1,0] = new Color(0f,1f,0f,1f);
     segclrs[2,1,1] = new Color(0.729f,0.333f,0.824f,1f); // MediumOrchid
     
     // Here would determine start and end positions for segments from data:

     segpos = new Vector3[trkcnt,maxsegs,2];  // X Z Y values 
     
     // track 0, with 1 segment:
     
     segpos[0,0,0] = new Vector3(1f,1f,1f);
     segpos[0,0,1] = new Vector3(0f,0.5f,-1f);

     // track 1, with 3 segments:
     
     segpos[1,0,0] = new Vector3(-1f,-1f,-1f);
     segpos[1,0,1] = new Vector3(0f,0.75f,-0.5f); // following start matches
     segpos[1,1,0] = new Vector3(0f,0.75f,-0.5f); // previous end
     segpos[1,1,1] = new Vector3(0.5f,0.75f,0f);
     segpos[1,2,0] = new Vector3(0.5f,0.75f,0f); 
     segpos[1,2,1] = new Vector3(0.375f,0.375f,0f);
 
             // track 2, with 2 segments:
     
     segpos[2,0,0] = new Vector3(0f,0f,0f);
     segpos[2,0,1] = new Vector3(0.75f,0.5f,0.375f);
     segpos[2,1,0] = new Vector3(0.75f,0.5f,0.375f); 
     segpos[2,1,1] = new Vector3(0.1f,-0.3f,-0.25f);
     
     // Create and load LineRenderers, attaching each to a gameObject:
     
     for (int i=0; i<trkcnt; i++) {
         
         int segcnt = segcnts[i];
         
         for (int j=0; j<segcnt; j++) {     
             
                // tracks[i,j] = GetComponent<LineRenderer>() as LineRenderer;
                //   tracks[i,j] = GetComponent<LineRenderer>();
         
             
             tracks[i,j] = gameObject.AddComponent<LineRenderer>(); // as 
                   LineRenderer;  
              
             tracks[i,j].SetVertexCount(2);
             tracks[i,j].SetColors(segclrs[i,j,0], segclrs[i,j,1]);
                     tracks[i,j].SetWidth(0.1f, 0.1f);
                     tracks[i,j].material = new   
                             Material(Shader.Find("Particles/Additive"));
             tracks[i,j].enabled = true;  
 
             
         }    

     }
     
 }
 
 // Update is called once per frame
 void Update () {
     
     for (int i=0; i<trkcnt; i++) {
         
         int segcnt = segcnts[i];
         
         for (int j=0; j<segcnt; j++) {     
             tracks[i,j].SetPosition(0, segpos[i,j,0]);
                    tracks[i,j].SetPosition(1, segpos[i,j,1]);
         }
         
     }

 }
 

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by waterdog · Aug 28, 2013 at 06:58 AM

OK...Answering my own question. If anyone can point out improvements I'd be happy to see them. I cut my code from above down to it's most basic form and dropped all arrays in favor of hard coded parameters. Then, with enough head scratching and googling I finally put together all the pieces that never quite seem to show up all in one place at one time in all the available code snippets...;^)

The solution was to use empty GameObjects and attach a single LineRenderer to each of them. That much was pretty clear early on. But, how to actually do it...not so much. Here is what I came up with. It requires just an empty GameObject in the Inspector to attach the script to. The script handles everything else. It renders two line segments end to end with a continuous color gradient from the start of the first to the end of the second.

using UnityEngine;

public class HardCodeTest : MonoBehaviour {

 public LineRenderer track0;
 public LineRenderer track1;    
 public GameObject gameObj0;
 public GameObject gameObj1;
 
 public Color Black = new Color(0f,0f,0f,1f); 
 public Color Coral = new Color(1f,0.498f,0.314f,1f);   
 public Color Cyan = new Color(0f,1f,1f,1f);
 public Color Lime = new Color(0f,1f,0f,1f);
 public Color MediumOrchid = new Color(0.729f,0.333f,0.824f,1f);  
 public Color Red = new Color(1f,0f,0f,1f);
 
  void Start () {
     
      gameObj0 = new GameObject("track0_GO");
     
      track0 = gameObj0.AddComponent<LineRenderer>();
      track0.SetVertexCount(2);
      track0.SetColors(Red, MediumOrchid);
      track0.SetWidth(0.7f, 0.1f);
      track0.material = new Material(Shader.Find("Particles/Additive"));
      track0.enabled = true; 
 
      gameObj1 = new GameObject("track1_GO");
     
      track1 = gameObj1.AddComponent<LineRenderer>();
      track1.SetVertexCount(2);
      track1.SetColors(MediumOrchid, Cyan);
      track1.SetWidth(0.1f, 0.7f);
      track1.material = new Material(Shader.Find("Particles/Additive"));
      track1.enabled = true; 
   
 }
 
 void Update () {
     
     track0.SetPosition(0, new Vector3(0f,0f,0f));
     track0.SetPosition(1, new Vector3(0.75f,0.5f,0.375f));

     track1.SetPosition(0, new Vector3(0.75f,0.5f,0.375f));
       track1.SetPosition(1, new Vector3(1.75f,0.5f,-0.375f));    
     
 } 
 

}

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Is it possible to update variable in Coroutine? 1 Answer

Problem with Web GL Build 1 Answer

How to let multiple values contribute two one value constantly 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges