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

Creating a LineRenderer: its always null

I am attempting to create a LineRenderer on the fly(at run time). But when I declare a LineRenderer its always null for some reason. I am using a C# script to create the object

What am I doing wrong? Can you suggest what needs changing?

 void OnGUI() {
     if (GUI.Button (new Rect(10,10,100,20), "Create")) {
     LineRenderer lRend = (LineRenderer)GameObject.Instantiate(new LineRenderer());
     // I've also tried the below
     // LineRenderer = new LineRenderer();
     Debug.Log ("IS NULL"+(lRend == null).ToString ()); // its always NULL??
     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]);
     }
 }
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Amanna · Apr 12, 2016 at 04:42 PM

 GameObject gObject = new GameObject("MyGameObject");
 LineRenderer lRend = gObject.AddComponent<LineRenderer>();
 
 lRend.SetColors (Color.red,Color.blue);
 lRend.material = new Material(Shader.Find("Particles/Additive"));
 lRend.SetWidth(1, 1);
 lRend.SetPosition(0,Vector3.zero);
 lRend.SetPosition(1,Vector3.one);


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
avatar image
0

Answer by rutter · Jun 15, 2012 at 05:15 AM

This line:

 LineRenderer lRend = (LineRenderer)GameObject.Instantiate(new LineRenderer());

Could be much simpler:

 LineRenderer lRend = new LineRenderer();
Comment
Add comment · Show 1 · 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 Mizuho · Jun 15, 2012 at 05:39 AM 1
Share

This will make the LineRenderer null. I just tried it. You need to create an empty GameObject and attach the LineRenderer to that.

 GameObject newLine = new GameObject("Line");
 LineRenderer lRend = newLine.AddComponent<LineRenderer>();

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

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

Line renderer corner issue (Gui Shader) 0 Answers

C# create linerenderer line copy based on spectrum data 2 Answers

Object follows a moving ellipse 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