• 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
Question by JakeMNew · Jun 15, 2012 at 05:05 AM · c#linerenderer

Wont allow me to create more than 1 LineRenderer

I am attempting to create LineRenderers at runtime(when the user presses a button).

My Problem: I can never create more than one LineRenderer. When I go to created the 2nd one, the LineRenderer object is always NULL.

What am I doing wrong? Can you provide advice on what I need to do to create more than one LineRenderer?

 public class AppInit : MonoBehaviour {
     
     public Vector3[] TEST_VERTICES;
     public const int SPEED = 5;
     public List<LineRenderer> lines;
     
     // Use this for initialization
     void Start () {
         TEST_VERTICES = new Vector3[10] {new Vector3(0,0,0), new Vector3(10,10,10), new Vector3(30,10,50), new Vector3(30,40,50), 
                                          new Vector3(10,30,90), new Vector3(10,20,40), new Vector3(50,20,40), new Vector3(70,80,90), 
                                          new Vector3(10,70,20), new Vector3(60,10,0)};
         lines = new List<LineRenderer>();
     }
     
     // Update is called once per frame
     void Update () {
         float x = Input.GetAxis ("Horizontal") * Time.deltaTime * SPEED;
         float z = 0;
         float y = Input.GetAxis ("Vertical")   * Time.deltaTime * SPEED;
         
         gameObject.transform.Translate (new Vector3(x,y,z));
     }
     
     void OnGUI() {
         if (GUI.Button (new Rect(10,10,100,20), "Create"))
             createString(TEST_VERTICES);
     }
     
     public bool createString( Vector3[] vertices ) {
         LineRenderer lRend = gameObject.AddComponent ("LineRenderer") as LineRenderer;
         //LineRenderer lRend = new LineRenderer();
         lines.Add(lRend);
         Debug.Log ("IS NULL"+(lRend == null).ToString ());
         lRend.SetColors (new Color(100,0,0,100), new Color(0,0,100,100));
         lRend.SetWidth(10, 1);
         lRend.SetVertexCount(vertices.Length);
         
         for (int i=0; i<vertices.Length; i++)
             lRend.SetPosition(i, vertices[i]);
         
         return true;
     }
 }
Comment
vt5491

People who like this

1 Show 0
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

Answer by Bunny83 · Jun 15, 2012 at 09:24 AM

It should be possible to attach more than one linerenderer to one gameobject, but in your case all are drawing exactly the same... You set the exact same vertices array. It depends on the LineRenderer-useWorldSpace setting if the positions are relative to the objects position, so if you move the gameobject the line moves along, or relative to the world. In both cases your multiple lines will be exactly the same...

What do you want to achieve? a TrailRenderer?

btw, there is a generic version of AddComponent:

 LineRenderer lRend = gameObject.AddComponent<LineRenderer>();


Comment

People who like this

0 Show 2 · 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
avatar image Fattie · Jun 15, 2012 at 01:23 PM 1
Share

LineRenderer is a component. I'm pretty sure you can only have one LineRenderer per game object.

In the editor you can only attach one, and I just tried it in code, you can only attach one.

Also I recall it mentions it in the doc somewhere: but I can't find it

(Aside: in fact, is there a list or something of "components where you can attach only one to a game object" ? Eg Transform etc??)

avatar image Fattie · Jun 15, 2012 at 01:24 PM 2
Share

Actually it says unclearly here

http://unity3d.com/support/documentation/Components/class-LineRenderer.html

"to draw two or more completely separate lines, you should use multiple GameObjects, each with its own Line Renderer"

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How do I replace "SetVertexCount"? 1 Answer

Setting Line Render position directly forward from facing of game object 1 Answer

Rotating a LineRenderer 0 Answers


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