LineRenderer.SetPosition index out of bounds! Help!
when I run this code it gives me the error:
LineRenderer.SetPosition index out of bounds! UnityEngine.LineRenderer:SetPosition(Int32, Vector3) raycast:Update() (at Assets/Keep/Pack/raycast.cs:66)
here is the code:
using UnityEngine; using System.Collections;
public class raycast : MonoBehaviour {
public int gunDamage = 1;
public float fireRate = 0.25f;
public float weaponRange = 50f;
public float hitForce = 100f;
public Transform gunEnd;
public Camera fpsCam;
private WaitForSeconds shotDuration = new WaitForSeconds(0.07f);
private AudioSource gunAudio;
private LineRenderer laserLine;
private float nextFire;
void Start ()
{
laserLine = GetComponent<LineRenderer>();
gunAudio = GetComponent<AudioSource>();
}
void Update ()
{
if (Input.GetButtonDown("Fire1") && Time.time > nextFire)
{
nextFire = Time.time + fireRate;
StartCoroutine (ShotEffect());
Vector3 rayOrigin = fpsCam.ViewportToWorldPoint (new Vector3(0.5f, 0.5f, 0.0f));
RaycastHit hit;
Debug.DrawRay(transform.position, rayOrigin, Color.green);
laserLine.SetPosition (1, gunEnd.position);
if (Physics.Raycast (rayOrigin, fpsCam.transform.forward, out hit, weaponRange))
{
laserLine.SetPosition (0, hit.point);
ShootableBox health = hit.collider.GetComponent<ShootableBox>();
if (health != null)
{
health.Damage (gunDamage);
}
if (hit.rigidbody != null)
{
hit.rigidbody.AddForce (-hit.normal * hitForce);
}
}
else
{
laserLine.SetPosition (1, rayOrigin + (fpsCam.transform.forward * weaponRange));
}
}
}
private IEnumerator ShotEffect()
{
gunAudio.Play ();
laserLine.enabled = true;
yield return shotDuration;
laserLine.enabled = false;
}
}
Answer by JustNatBros · Aug 10, 2017 at 12:22 PM
@spacechicken27 I had the same error, but I think I fixed it. Try and change the size under Positions on you're Line Renderer to 2. This fixed the error for me.
Answer by Tulkas1 · May 02, 2018 at 09:19 PM
Having the same issue, and now I'm giving up on searching for answers for now, because this is just aggrivating. I did what you said, nothing really changed.
Are you sure you read JustNatBros's answer? spacechicken27 seems to be a bad observer as what he describes sounds like he actually did change the point count at runtime which made the error go away during the play session but when he stopped it was back to the original value.
You posted an answer but you did not answer the question. You should have posted a comment.
Answer by spacechicken27 · Apr 18, 2017 at 03:54 AM
Well, When messing around I added a mesh collider to my gun and made sure the gunEnd was far enough away from the gun, and it worked... somehow. I did not change any code.
IMPORTANT REVISION!! after just playing with it for a few minutes seeing how it behaved when I aimed in different ways, I stopped running the program, ran it again, and the error appeared again!!! i did not change anything!
Your answer
Follow this Question
Related Questions
Adding points for every gameObject on the scene instead of adding points for the destroyed one 0 Answers
Cant Kill enemy with Raycast C# (SOLVED) 1 Answer
RayCast From Centre of Camera 1 Answer
How to change the position of a Raycast (on Center of the Player 2D)? 0 Answers
Inconsistent Jump Heights 1 Answer