• 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 Landrade1093 · Oct 09, 2015 at 02:00 PM · nullreferenceexceptionlinerendererscritping

Null LineRenderer in gameobjects with the same script

I'm having an issue where the line renderer is null in certain objects, even though the objects are of the same type. In other words, these objects all have the same script that holds a line renderer, but some have a null line renderer and others don't. The Start method adds the line renderer component to each object and stores the reference, so I know that the line is being made. But for some reason, certain objects still have a null line renderer when the line is used in another method. Here is the code that sets up the line renderer:

     public List<Checkpoint> Checkpoints;
     public LineRenderer lineRenderer;
 
     void Start()
     {
         lineRenderer = gameObject.AddComponent<LineRenderer>();
         lineRenderer.SetVertexCount(Checkpoints.Count);
         lineRenderer.SetWidth(0.5f, 0.5f);
         for (int i = 0; i < Checkpoints.Count; i++)
         {
             lineRenderer.SetPosition(i, Checkpoints[i].GetPosition());
         }
         lineRenderer.enabled = false;
     }

Here is the code that shows the line:

     void Update()
     {
         if (lineRenderer.enabled)
         {
             for (int i = 0; i < Checkpoints.Count; i++)
             {
                 lineRenderer.SetPosition(i, Checkpoints[i].GetPosition());
             }
         }
     }
 
     public void DisplayLine(bool display)
     {
         if (lineRenderer == null)
         {
             lineRenderer = gameObject.GetComponent<LineRenderer>();
         }
 
         lineRenderer.enabled = display;
     }

The issue is in the DisplayLine method. For some reason, when I call this method, lineRenderer is null, even thought I initialize it on Start() and the debugger tells me that lineRenderer is initialized. Even the GetComponent() method returns null when the Inspector shows that the component is on the game object. This issue is only happening with certain objects and I'm not sure how to fix it.

Comment
Add comment · Show 1
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 meat5000 ♦ · Oct 09, 2015 at 02:47 PM 0
Share

If you need to use GetComponent in the DisplayLine function, why do you not need it in Start() or Update()?

$$anonymous$$ove the GetComponent to Start() underneath your addcomponent adnd see if there is any difference whatsoever.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by FortisVenaliter · Oct 09, 2015 at 02:42 PM

It sounds like DisplayLine() is being called before the object has properly instantiated. That would explain why GetComponent also failed. You need to make sure the object has called Start() before you ever call DisplayLine().

Comment
Add comment · 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 Landrade1093 · Oct 09, 2015 at 04:35 PM 0
Share

DisplayLine() is not called until much later in the game. The game involves that the player "mark" the line in order to display it, so I know that the method is not being called until a certain player action has been done. Thanks for the advice though. Anything else you might think is the problem? Every object using this script for the line follows the same process in the game, so it seems weird that some objects have an initialized line renderer and others don't, even though the debugger says they are all instantiated. Could it have something to do with "lineRenderer.enabled = false" in the Start() method?

avatar image FortisVenaliter Landrade1093 · Oct 09, 2015 at 06:40 PM 0
Share

No, that wouldn't eli$$anonymous$$ate the reference. Are any of your objects deactivated? If so, the start function won't be called, and the linerenderer may not be initialized.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Filling area under positions with Mesh 0 Answers

LineRenderer renders the wrong color 3 Answers

Unity C#: Line renderer from Gameobject (3D) to Canvas (Screen Space - Camera) 1 Answer

i want change color in line renderer. 1 Answer

Apply a LineRenderer to surface of model 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