Survival Shooter LineRenderer.setPosition index out of bounds
I am following the survival shooter tutorial, and am having issues. I would like to have all errors corrected, but it really isn't impacting gameplay that much. I added a second element under "Materials", but that hasn't changed anything. I know my approach to solving these issues probably needs to change, but this is all very new to me, so could I get some help with this issue? Here is the code
using UnityEngine;
public class PlayerShooting : MonoBehaviour { public int damagePerShot = 20; public float timeBetweenBullets = 0.15f; public float range = 100f;
float timer;
Ray shootRay = new Ray();
RaycastHit shootHit;
int shootableMask;
ParticleSystem gunParticles;
LineRenderer gunLine;
AudioSource gunAudio;
Light gunLight;
float effectsDisplayTime = 0.2f;
void Awake ()
{
shootableMask = LayerMask.GetMask ("Shootable");
gunParticles = GetComponentInChildren<ParticleSystem> ();
gunLine = GetComponentInChildren <LineRenderer> ();
gunAudio = GetComponentInChildren<AudioSource> ();
gunLight = GetComponentInChildren<Light> ();
}
void Update ()
{
timer += Time.deltaTime;
if(Input.GetButton ("Fire1") && timer >= timeBetweenBullets && Time.timeScale != 0)
{
Shoot ();
}
if(timer >= timeBetweenBullets * effectsDisplayTime)
{
DisableEffects ();
}
}
public void DisableEffects ()
{
gunLine.enabled = false;
gunLight.enabled = false;
}
void Shoot ()
{
timer = 0f;
gunAudio.Play ();
gunLight.enabled = true;
gunParticles.Stop ();
gunParticles.Play ();
gunLine.enabled = true;
gunLine.SetPosition (0, transform.position);
shootRay.origin = transform.position;
shootRay.direction = transform.forward;
if(Physics.Raycast (shootRay, out shootHit, range, shootableMask))
{
EnemyHealth enemyHealth = shootHit.collider.GetComponent <EnemyHealth> ();
if(enemyHealth != null)
{
enemyHealth.TakeDamage (damagePerShot, shootHit.point);
}
gunLine.SetPosition (1, shootHit.point);
}
else
{
gunLine.SetPosition (1, shootRay.origin + shootRay.direction * range);
}
}
}
Answer by pokitman12 · May 02, 2018 at 10:21 PM
Hi. I've tested your code and couldn't find a problem and didn't get any errors. I did however get the error when changing the positions length from 2 to 1 on the line renderer in the inspector which would course out of bounds error. check if your positions length is set to 2 in the inspector.
Your answer
![](https://koobas.hobune.stream/wayback/20220612161929im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Any way to save a trail renderer? 1 Answer
How to change Line Renderer facing 0 Answers
Can't get a smooth following line 1 Answer