Help with error on angry birds tutorial

I have an error its with the angry bird tutorial part 1, I get this error when I start the game.
the rubber band does not connect to the asteroid and this message pops up

LineRenderer.SetPosition index out of bounds!
UnityEngine.LineRenderer:SetPosition(Int32, Vector3)
Projectile_dragging:LineRendererSetup() (at Assets/Scripts/Projectile_dragging.cs:59)
Projectile_dragging:Start() (at Assets/Scripts/Projectile_dragging.cs:24)

its something to do with the line renderer void??

void LineRendererSetup () {
catapultLineFront.sortingLayerName = “Foreground”;
catapultLineBack.sortingLayerName = “Foreground”;
catapultLineFront.SetPosition(0, catapultLineFront.transform.position);
catapultLineBack.SetPosition(0, catapultLineBack.transform.position);
catapultLineFront.sortingOrder = 3;
catapultLineBack.sortingOrder = 1;
}
void OnMouseDown() {
spring.enabled = false;
clickedOn = true;
}
void OnMouseUp(){
spring.enabled = true;
rigidbody2D.isKinematic = false;
clickedOn = false;
}
Please help im very confused and thanks in advance

:roll_eyes:

A couple of things.

First off, can you follow the link I sent you regarding how to post code and using code tags properly. You can edit your post and add the proper code tags so we can read your code.

Second, watch the video carefully and check to see if there are any differences between the code you have written above and the code in the video.

Third, you should understand how to read errors. Double clicking the error in the console will take you to the line creating the error. The error tells you which lines in the script and at what character in that line the error is generated.

All of these will help you troubleshoot your code.

When your code is formatted, I’ll give it a read through and see if I can help more.

using UnityEngine;
using System.Collections;
public class Projectile_dragging : MonoBehaviour {
 public float maxStretch = 3.0f;
 public LineRenderer catapultLineFront;
 public LineRenderer catapultLineBack;
 private SpringJoint2D spring;
 private Transform catapult;
 private Ray rayToMouse; 
 private Ray leftCatapultToProjectile;
 private float maxStretchSqr;
 private bool clickedOn;
 private float circlRadius;
 private Vector2 prevVelocity;
 void Awake(){
 spring = GetComponent <SpringJoint2D> ();
 catapult = spring.connectedBody.transform;
 }
 void Start () {
 LineRendererSetup ();
 rayToMouse = new Ray(catapult.position, Vector3.zero);
 leftCatapultToProjectile = new Ray(catapultLineFront.transform.position, Vector3.zero);
 maxStretchSqr = maxStretch * maxStretch;
 CircleCollider2D circle = collider2D as CircleCollider2D;
 circlRadius = circle.radius;
 }
 void Update () {
 if (clickedOn)
 Dragging ();
 if (spring != null) {
 if (!rigidbody2D.isKinematic && prevVelocity.sqrMagnitude > rigidbody2D.velocity.sqrMagnitude) {
 Destroy (spring);
 rigidbody2D.velocity = prevVelocity;
 }
 if(!clickedOn)
 prevVelocity = rigidbody2D.velocity;
 } else {
 catapultLineFront.enabled = false;
 catapultLineBack.enabled = false;
 }
 
 }

 void LineRendererSetup () {

 catapultLineFront.SetPosition(0, catapultLineFront.transform.position);
 catapultLineBack.SetPosition(0, catapultLineBack.transform.position);
 catapultLineFront.sortingLayerName = "Foreground";
 catapultLineBack.sortingLayerName = "Foreground";
 catapultLineFront.sortingOrder = 3;
 catapultLineBack.sortingOrder = 1;
 }
 void OnMouseDown() {
 spring.enabled = false;
 clickedOn = true;
 }
 void OnMouseUp(){
 spring.enabled = true;
 rigidbody2D.isKinematic = false;
 clickedOn = false;
 }
 void Dragging(){
  Vector3 mouseWorldPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition);
 Vector2 catapultToMouse = mouseWorldPoint - catapult.position;
 
 if (catapultToMouse.sqrMagnitude > maxStretchSqr){
 rayToMouse.direction = catapultToMouse;
 mouseWorldPoint = rayToMouse.GetPoint(maxStretch);
 }
 mouseWorldPoint.z = 0f;
 transform.position = mouseWorldPoint;
 }
 void LineRendererUpdtae(){
 Vector2 catapultToProjectile = transform.position - catapultLineFront.transform.position;
 leftCatapultToProjectile.direction = catapultToProjectile;
 Vector3 holdPoint = leftCatapultToProjectile.GetPoint (catapultToProjectile.magnitude + circlRadius);
 catapultLineFront.SetPosition (1, holdPoint);
 catapultLineBack.SetPosition (1, holdPoint);
 }
}

thank you for all your help I will re watch the video a few times and try to find the error.
if you do get the chance to read my script please message me on your findings so I can continue with my own interpretation on angry birds. I am very new to coding so all your videos help me a lot on learning a lot more about c#

ps keep going with you live training sessions I think you should do a c# fps or rpg because there aren’t many tutorials out there although I do realise you have a lot of training sessions planed I think it would be a good idea for the future.

Ok: The point to the code tags is so you can maintain the formatting of the code.

This means wrapping code tags around properly formatted code. You would need to go back and paste new code, not just the broken code from the previous post… :-/

It should look something like this:

using UnityEngine;
using System.Collections;

public class Projectile_dragging : MonoBehaviour {
  public float maxStretch = 3.0f;
  public LineRenderer catapultLineFront;
  public LineRenderer catapultLineBack;

  private SpringJoint2D spring;
  private Transform catapult;
  private Ray rayToMouse;
  private Ray leftCatapultToProjectile;
  private float maxStretchSqr;
  private bool clickedOn;
  private float circlRadius;
  private Vector2 prevVelocity;

  void Awake(){
    spring = GetComponent <SpringJoint2D> ();
    catapult = spring.connectedBody.transform;
  }

  void Start () {
    LineRendererSetup ();
    rayToMouse = new Ray(catapult.position, Vector3.zero);
    leftCatapultToProjectile = new Ray(catapultLineFront.transform.position, Vector3.zero);
    maxStretchSqr = maxStretch * maxStretch;
    CircleCollider2D circle = collider2D as CircleCollider2D;
    circlRadius = circle.radius;
  }

  void Update () {
     if (clickedOn)
       Dragging ();
     if (spring != null) {
       if (!rigidbody2D.isKinematic && prevVelocity.sqrMagnitude > rigidbody2D.velocity.sqrMagnitude) {
         Destroy (spring);
         rigidbody2D.velocity = prevVelocity;
        }
        if(!clickedOn)
          prevVelocity = rigidbody2D.velocity;
     } else {
        catapultLineFront.enabled = false;
        catapultLineBack.enabled = false;
      }
    }



 // etc.. etc..

It might be good that you go to the learn site and run one of the basic tutorial there, like “Roll-a-Ball” to get a feel for making games in Unity.

It might also be good to go through the learning tutorials on beginner scripting, this way you’ll understand more about what you are doing. The class you are working with is “intermediate” when it comes to complexity, so you may need a better foundation before giving it a go.

Some things related to your errors:

  • The very first thing I would check is to see if you have populated your line renderers in the inspector. Could you be trying to run the game when you have not associated the line renderers to the public property?

Lastly - double check your spelling / typing as I see things like:

  • circlRadius not circleRadius
  • LineRendererUpdtae not LineRendererUpdate

These are not technically incorrect, but you will have to maintain this mis-spelling throughout the entire script for it to work. It is often better to make sure spelling is correct, so we don’t make the mistake of later spelling something correctly, and breaking the script.

Speaking of LineRendererUpdate() I don’t see it being called anywhere in the code you posted, so either the script is unfinished, or you have not posted the entire script.

Let me know how you get on and what else we can do to help.

what do u mean by not called I have this piece of code
void LineRendererUpdate()

a ha I found problem ur right again I hadn’t called linerenderer update but I have now it works properly but I still get out of bounds error

The out of bounds error means that the collection of points that make up the line is not long enough for what you are calling. Say you have a list or array with 10 elements. If you try to set element 21, you will get an out of bounds error, as 21 is not between 0 and 9 making the 10 elements in the collection.

You seem to be getting an out of bounds error in element 0. This leads me to believe you have not initialised your array.

As the array for the line renderer should be set when you populate the reference to the line renderer in the inspector, it makes me wonder if you have dragged the reference to the line renderers in the inspector. Just a guess.

You can double check against the video to be sure.

Give us a screenshot of your inspector and we can tell you.

here

and this

although u probably want this 1

Anychance you know what’s wrong

:sunglasses:Even If I can’t fix the error it’s not affecting my game in anyway it’s just in the console

do you know whats wrong?

I don’t understand why I have error with 1 band and not the other do you have any idea why this could be?

Please don’t keep bumping your posts as this is what will put people off answering them. Did you try what @Adam-Buckner_1 said about checking if you are initialising your array?