Script not showing Line Renderer in Game View

Hello,

I’m creating a Script in part of which the user will click and drag their mouse across the scene. A line renderer in a child object creates a line connecting the place the mouse first pressed down to its current position. Here’s the script, having removed the parts that don’t pertain to the line:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerAim : MonoBehaviour
{
    bool updatedOrigin=false;
    Vector3 origin;
    Vector3 mousePos;

    LineRenderer line;

    void Start(){
       line=GetComponentInChildren<LineRenderer>();
    }

    void FixedUpdate(){

        if(Input.GetMouseButton(0)){//if mouse pressed...;
                mousePos=Camera.main.ScreenToWorldPoint(Input.mousePosition);//get coordinates of mouse
          if(updatedOrigin==false){//sets the origin at the position of mouse during the first frame at which the mouse is pressed
            origin=mousePos;        
            updatedOrigin=true;
            line.enabled=true; //enable line when mouse clicked
          }

          line.SetPosition(1,origin);
          line.SetPosition(0,mousePos);//set position of line

        }
        if (Input.GetMouseButtonUp(0)){
          updatedOrigin=false;//makes the origin able to be reset when mouse is released, resets vars
          line.enabled=false; // disable line when mouse released
        }

    }
}

The script uses FixedUpdate() because there are other parts of the script irrelevant to the line renderer which involve physics.
However, for some reason this script is causing the line renderer to become invisible in the Game View. I know that it is the script causing the issue (as opposed to an error in the sorting layer, etc.) because the line appears if the script is disabled. I also know the script otherwise does what it is supposed to , because the line appears as it should in the scene view.

To summarize, the script mostly does what it is supposed to, but makes the line invisible only in the Game View. Does anyone know why this is happening, and how to fix it?

Thanks so much and have a great day!

You’ll need to move the code starting from line 31 out of FixedUpdate. Input.GetMouseButtonUp is only true on the exact frame the mouse button was released, but FixedUpdate is not called every frame. So it will sometimes miss when the mouse button is released. But that’s not the issue you’re here for.

Are you seeing anything in the console? What I’m suspicious of is it looks like your LineRenderer is starting in a disabled state. But I have doubts that GetComponentInChildren will find a disabled component. Whether it does or not is not mentioned in the documentation. If line 14 fails, then line 24 generates a null reference error, and you never see the line. (but I’m probably wrong, and don’t have Unity in front of me to check)

The line is showing up in the scene view, so its not attempting to enable a null reference, otherwise it wouldn’t show up at all. I am not getting any consul errors, and the other tasks I’m having it do in line 31 (mostly Instantiating an arrow prefab) are working properly, so it’s not that either…

However, I’m now pretty sure it has to do with my code for setting the position of the line.
If I delete lines 27-28, the line will appear in the game view as it should (of course, just not changing position)

I got it working! Turns out, it was an issue with the mouse for some reason taking z coordinates other than 0. I changed lines 27-28 to line.SetPosition(1,new Vector3(origin.x,origin.y,0)); and line.SetPosition(0,new Vector3(mousePos.x,mousePos.y,0));
and that fixed the issue. Thanks for your help!