- Home /
LineRenderer missing in build
Hi, I have the problem that I found out few other people have had before me too but nobody seems to come up with actual solution or at least in every case those have been very different from each others.
So the thing is that I'm using LineRenderer to draw bullet lines and it works nicely in the editor but not at all in the build (I'm using win7, Unity4.3.0f4, 3d). Some people have solved this by lowering or raising their render settings, others by creating an extra material in the script but not actually even using it (http://answers.unity3d.com/questions/575167/linerenderer-not-showing-in-build.html) and for some people it has just suddenly started to work on its own. For me these won't seem to happen. Am I missing something? Help would be highly appreciated, I'm getting desperate :/
Here is the code that I'm using at the moment:
`using UnityEngine;
using System.Collections;
public class Gun : MonoBehaviour {
public LayerMask collisionMask;
public Transform spawn;
public GameObject gun;
public float timeBetweenShots = 0.3f;
public float damage = 1.0f;
private float nextPossibleShootTime;
private LineRenderer tracer;
private float distance;
private bool revealed;
void Start(){
tracer = GetComponent<LineRenderer>();
revealed = false;
}
void Update(){
if(Input.GetButtonDown("Fire2"))
Shoot();
distance = Vector3.Distance(GameObject.FindGameObjectWithTag("Prey").transform.position, GameObject.FindGameObjectWithTag("Predator").transform.position);
if(distance<=30)
revealed=true;
}
void Shoot(){
if(CanShoot()){
Ray ray = new Ray(spawn.position, -spawn.up);
RaycastHit hit;
Transform hitTransform;
Vector3 hitPoint;
hitTransform = FindClosestHitObject (ray, out hitPoint);
float shotDistance = 25;
if(Physics.Raycast(ray, out hit, shotDistance, collisionMask)){
shotDistance = hit.distance;
if(hitTransform != null)
Debug.Log("We hit: " + hitTransform.name);
if(hit.collider.GetComponent<Entity>()){
hit.collider.GetComponent<Entity>().TakeDamage(damage);
}
}
StartCoroutine("RenderTracer", ray.direction * shotDistance);
nextPossibleShootTime = Time.time + timeBetweenShots;
}
}
Transform FindClosestHitObject (Ray ray, out Vector3 hitPoint){
RaycastHit[] hits = Physics.RaycastAll (ray);
Transform closestHit = null;
float distance = 0;
hitPoint = Vector3.zero;
foreach (RaycastHit hit in hits) {
if(hit.transform != this.transform && (closestHit==null || hit.distance < distance)){
closestHit = hit.transform;
distance = hit.distance;
hitPoint = hit.point;
}
}
return closestHit;
}
private bool CanShoot(){
bool canShoot = true;
if((Time.time < nextPossibleShootTime) || (!revealed)){
canShoot = false;
}
return canShoot;
}
IEnumerator RenderTracer(Vector3 hitPoint){
tracer.enabled=true;
tracer.SetPosition(0, spawn.position);
tracer.SetPosition(1, spawn.position + hitPoint);
yield return null;
tracer.enabled = false;
}
}`
Most of this code is from Sebastian Lague's youtube Top Down Shooter tutorials, some from quill18creates youtube tutorials and some even my own :)
Answer by Kitai · Mar 13, 2014 at 07:33 AM
Ok, it seems to be working now. I created whole new script and did basically all the same things as above. I even copy&pasted some of the lines to the new script. And of, I deleted the LineRenderer component as well and readded it just to make sure there is nothing weird going on because of that.
Now that I think about it, there might have been some invisible symbols or letters in my script that I would had added by mistake. So maybe copy&paste to notepad and back to MonoDevelop (which at least I'm using) could work? I didn't try thou, just an idea.
Just in case someone wants to compare these two scripts, here is the one that I'm using now:
using UnityEngine;
using System.Collections;
public class Shooting : MonoBehaviour {
public LayerMask collisionMask;
public Transform spawn;
public float fireRate = 0.5f;
public float damage = 25f;
float cooldown = 0;
private LineRenderer tracer;
private float distance;
private bool revealed;
void Start(){
tracer = GetComponent<LineRenderer>();
revealed = false;
}
// Update is called once per frame
void Update () {
cooldown -= Time.deltaTime;
distance = Vector3.Distance(GameObject.FindGameObjectWithTag("Prey").transform.position, GameObject.FindGameObjectWithTag("Predator").transform.position);
if(distance <= 30)
revealed=true;
if ((Input.GetButtonDown ("Fire1") || Input.GetButtonDown ("Fire2")) && revealed) {
Fire();
}
}
void Fire(){
if (cooldown > 0) {
return;
}
RaycastHit hit;
Ray ray = new Ray (spawn.position, -spawn.up);
Transform hitTransform;
Vector3 hitPoint;
float shotDistance = 25;
hitTransform = FindClosestHitObject (ray, out hitPoint);
if(Physics.Raycast(ray, out hit, shotDistance, collisionMask)){
shotDistance = hit.distance;
if(hitTransform != null)
Debug.Log("We hit: " + hitTransform.name);
if(hit.collider.GetComponent<Entity>()){
hit.collider.GetComponent<Entity>().TakeDamage(damage);
}
}
StartCoroutine("RenderTracer", ray.direction * shotDistance);
cooldown = fireRate;
}
Transform FindClosestHitObject (Ray ray, out Vector3 hitPoint){
RaycastHit[] hits = Physics.RaycastAll (ray);
Transform closestHit = null;
float distance = 0;
hitPoint = Vector3.zero;
foreach (RaycastHit hit in hits) {
if(hit.transform != this.transform && (closestHit==null || hit.distance < distance)){
closestHit = hit.transform;
distance = hit.distance;
hitPoint = hit.point;
}
}
return closestHit;
}
IEnumerator RenderTracer(Vector3 hitPoint){
tracer.enabled=true;
tracer.SetPosition(0, spawn.position);
tracer.SetPosition(1, spawn.position + hitPoint);
yield return null;
tracer.enabled = false;
}
}
BTW - You should be able to accept your solution, tick the 'Accept Answer' icon below the thumbs (not the vote up/down)
Oh, look at that. I though that without enough karma I couldn't do even that. Thanks!
Your answer
Follow this Question
Related Questions
Line renderer jagged display issue 0 Answers
How do I render the inside of a transparent model (a glass jar)? 1 Answer
Player position changes lighting. 0 Answers
URP material set texture scale 0 Answers
overlaying object problem 0 Answers