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!