Search Unity

Help with error on angry birds tutorial

Discussion in 'Scripting' started by jm1001, Jul 29, 2014.

Thread Status:
Not open for further replies.
  1. jm1001

    jm1001

    Joined:
    Jul 29, 2014
    Posts:
    14
    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
     
  2. jm1001

    jm1001

    Joined:
    Jul 29, 2014
    Posts:
    14
  3. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    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.
     
  4. jm1001

    jm1001

    Joined:
    Jul 29, 2014
    Posts:
    14
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. public class Projectile_dragging : MonoBehaviour {
    5.  public float maxStretch = 3.0f;
    6.  public LineRenderer catapultLineFront;
    7.  public LineRenderer catapultLineBack;
    8.  private SpringJoint2D spring;
    9.  private Transform catapult;
    10.  private Ray rayToMouse;
    11.  private Ray leftCatapultToProjectile;
    12.  private float maxStretchSqr;
    13.  private bool clickedOn;
    14.  private float circlRadius;
    15.  private Vector2 prevVelocity;
    16.  void Awake(){
    17.  spring = GetComponent <SpringJoint2D> ();
    18.  catapult = spring.connectedBody.transform;
    19.  }
    20.  void Start () {
    21.  LineRendererSetup ();
    22.  rayToMouse = new Ray(catapult.position, Vector3.zero);
    23.  leftCatapultToProjectile = new Ray(catapultLineFront.transform.position, Vector3.zero);
    24.  maxStretchSqr = maxStretch * maxStretch;
    25.  CircleCollider2D circle = collider2D as CircleCollider2D;
    26.  circlRadius = circle.radius;
    27.  }
    28.  void Update () {
    29.  if (clickedOn)
    30.  Dragging ();
    31.  if (spring != null) {
    32.  if (!rigidbody2D.isKinematic && prevVelocity.sqrMagnitude > rigidbody2D.velocity.sqrMagnitude) {
    33.  Destroy (spring);
    34.  rigidbody2D.velocity = prevVelocity;
    35.  }
    36.  if(!clickedOn)
    37.  prevVelocity = rigidbody2D.velocity;
    38.  } else {
    39.  catapultLineFront.enabled = false;
    40.  catapultLineBack.enabled = false;
    41.  }
    42.  
    43.  }
    44.  
    45.  void LineRendererSetup () {
    46.  
    47.  catapultLineFront.SetPosition(0, catapultLineFront.transform.position);
    48.  catapultLineBack.SetPosition(0, catapultLineBack.transform.position);
    49.  catapultLineFront.sortingLayerName = "Foreground";
    50.  catapultLineBack.sortingLayerName = "Foreground";
    51.  catapultLineFront.sortingOrder = 3;
    52.  catapultLineBack.sortingOrder = 1;
    53.  }
    54.  void OnMouseDown() {
    55.  spring.enabled = false;
    56.  clickedOn = true;
    57.  }
    58.  void OnMouseUp(){
    59.  spring.enabled = true;
    60.  rigidbody2D.isKinematic = false;
    61.  clickedOn = false;
    62.  }
    63.  void Dragging(){
    64.   Vector3 mouseWorldPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    65.  Vector2 catapultToMouse = mouseWorldPoint - catapult.position;
    66.  
    67.  if (catapultToMouse.sqrMagnitude > maxStretchSqr){
    68.  rayToMouse.direction = catapultToMouse;
    69.  mouseWorldPoint = rayToMouse.GetPoint(maxStretch);
    70.  }
    71.  mouseWorldPoint.z = 0f;
    72.  transform.position = mouseWorldPoint;
    73.  }
    74.  void LineRendererUpdtae(){
    75.  Vector2 catapultToProjectile = transform.position - catapultLineFront.transform.position;
    76.  leftCatapultToProjectile.direction = catapultToProjectile;
    77.  Vector3 holdPoint = leftCatapultToProjectile.GetPoint (catapultToProjectile.magnitude + circlRadius);
    78.  catapultLineFront.SetPosition (1, holdPoint);
    79.  catapultLineBack.SetPosition (1, holdPoint);
    80.  }
    81. }
    82.  
     
  5. jm1001

    jm1001

    Joined:
    Jul 29, 2014
    Posts:
    14
    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.
     
  6. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    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:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Projectile_dragging : MonoBehaviour {
    5.   public float maxStretch = 3.0f;
    6.   public LineRenderer catapultLineFront;
    7.   public LineRenderer catapultLineBack;
    8.  
    9.   private SpringJoint2D spring;
    10.   private Transform catapult;
    11.   private Ray rayToMouse;
    12.   private Ray leftCatapultToProjectile;
    13.   private float maxStretchSqr;
    14.   private bool clickedOn;
    15.   private float circlRadius;
    16.   private Vector2 prevVelocity;
    17.  
    18.   void Awake(){
    19.     spring = GetComponent <SpringJoint2D> ();
    20.     catapult = spring.connectedBody.transform;
    21.   }
    22.  
    23.   void Start () {
    24.     LineRendererSetup ();
    25.     rayToMouse = new Ray(catapult.position, Vector3.zero);
    26.     leftCatapultToProjectile = new Ray(catapultLineFront.transform.position, Vector3.zero);
    27.     maxStretchSqr = maxStretch * maxStretch;
    28.     CircleCollider2D circle = collider2D as CircleCollider2D;
    29.     circlRadius = circle.radius;
    30.   }
    31.  
    32.   void Update () {
    33.      if (clickedOn)
    34.        Dragging ();
    35.      if (spring != null) {
    36.        if (!rigidbody2D.isKinematic && prevVelocity.sqrMagnitude > rigidbody2D.velocity.sqrMagnitude) {
    37.          Destroy (spring);
    38.          rigidbody2D.velocity = prevVelocity;
    39.         }
    40.         if(!clickedOn)
    41.           prevVelocity = rigidbody2D.velocity;
    42.      } else {
    43.         catapultLineFront.enabled = false;
    44.         catapultLineBack.enabled = false;
    45.       }
    46.     }
    47.  
    48.  
    49.  
    50.  // etc.. etc..
     
  7. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    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?
     
  8. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    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.
     
  9. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    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.
     
  10. jm1001

    jm1001

    Joined:
    Jul 29, 2014
    Posts:
    14
    what do u mean by not called I have this piece of code
    void LineRendererUpdate()
     
  11. jm1001

    jm1001

    Joined:
    Jul 29, 2014
    Posts:
    14
    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
     
  12. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    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.
     
  13. jm1001

    jm1001

    Joined:
    Jul 29, 2014
    Posts:
    14
    here
     

    Attached Files:

  14. jm1001

    jm1001

    Joined:
    Jul 29, 2014
    Posts:
    14
    and this
     

    Attached Files:

  15. jm1001

    jm1001

    Joined:
    Jul 29, 2014
    Posts:
    14
    although u probably want this 1
     

    Attached Files:

  16. jm1001

    jm1001

    Joined:
    Jul 29, 2014
    Posts:
    14
    Anychance you know what's wrong
     
  17. jm1001

    jm1001

    Joined:
    Jul 29, 2014
    Posts:
    14
    :cool:Even If I can't fix the error it's not affecting my game in anyway it's just in the console
     
  18. jm1001

    jm1001

    Joined:
    Jul 29, 2014
    Posts:
    14
    do you know whats wrong?
     
  19. jm1001

    jm1001

    Joined:
    Jul 29, 2014
    Posts:
    14
    I don't understand why I have error with 1 band and not the other do you have any idea why this could be?
     
  20. Josh-Naylor

    Josh-Naylor

    Administrator

    Joined:
    Jul 1, 2014
    Posts:
    216
    Please don't keep bumping your posts as this is what will put people off answering them. Did you try what @Adam Buckner said about checking if you are initialising your array?
     
  21. jm1001

    jm1001

    Joined:
    Jul 29, 2014
    Posts:
    14
    im sorry for my spam of messages and if I put people off answering my questions but I don't know how to initialise the array because I don't have much experience I have now decided that I will try a more basic game I have done the roll a ball game like adam suggested and I am now inproving it and adding more levels to the game.
     
  22. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    It's hard to tell exactly what's going on, as - even with 3 screen shots - we don't see all of your components on the GameObject.

    I don't see exactly what's going wrong from the material you are showing my.

    All I can say is:

    - Try to rewatch the videos (both Pt1 and Pt2) and try to see where your project is different.
    - Check out the line renderer and see if every thing is set up correctly, as you should get at least an element 0
    - Make sure you don't have any typographical or other simple errors that could be throwing things off.
     
  23. gouthammannuru

    gouthammannuru

    Joined:
    Sep 9, 2015
    Posts:
    16
    Assets/Scripts/BlackBirdDrag.cs(43,41): error CS0120: An object reference is required to access non-static member `UnityEngine.Rigidbody2D.isKinematic'

    Assets/Scripts/BlackBirdDrag.cs(45,45): error CS0120: An object reference is required to access non-static member `UnityEngine.Rigidbody2D.velocity'

    Assets/Scripts/BlackBirdDrag.cs(48,60): error CS0120: An object reference is required to access non-static member `UnityEngine.Rigidbody2D.velocity'

    Assets/Scripts/BlackBirdDrag.cs(78,29): error CS0120: An object reference is required to access non-static member `UnityEngine.Rigidbody2D.isKinematic'

    Assets/Scripts/BlackBirdDrag.cs(33,43): error CS0119: Expression denotes a `type', where a `variable', `value' or `method group' was expected

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class BlackBirdDrag : MonoBehaviour {
    5.  
    6.     public float maxStreatch = 3.0f;
    7.     public LineRenderer catapultLineFront;
    8.     public LineRenderer catapultLineBack;
    9.  
    10.     private SpringJoint2D spring;
    11.     private Transform catapult;
    12.     private Ray rayToMouse;
    13.     private Ray leftCatapultToProjectile;
    14.     private float maxStretchSqr;
    15.     private bool clickedOn;
    16.     private float circlRadius;
    17.     private Vector2 prevVelocity;
    18.     //private GameObject circle;
    19.  
    20.  
    21.  
    22.     void Awake () {
    23.         spring = GetComponent <SpringJoint2D> ();
    24.         catapult = spring.connectedBody.transform;
    25.     }
    26.    
    27.     void Start () {
    28.  
    29.         LineRendererSetup ();
    30.         rayToMouse = new Ray(catapult.position, Vector3.zero);
    31.         leftCatapultToProjectile = new Ray(catapultLineFront.transform.position, Vector3.zero);
    32.         maxStretchSqr = maxStreatch * maxStreatch;
    33.         CircleCollider2D circle = Collider2D as CircleCollider2D;
    34.         circlRadius = circle.radius;
    35.     }
    36.    
    37.  
    38.     void Update () {
    39.         if(clickedOn)
    40.             Dragging ();
    41.  
    42.         if(spring != null) {
    43.             if(!Rigidbody2D.isKinematic && prevVelocity.sqrMagnitude > Rigidbody2D.velocity.sqrMagnitude) {
    44.                 Destroy (spring);
    45.                 Rigidbody2D.velocity = prevVelocity;
    46.             }
    47.             if(!clickedOn)
    48.                 prevVelocity = Rigidbody2D.velocity;
    49.  
    50.             LineRendererupdate ();
    51.  
    52.         }else{
    53.             catapultLineFront.enabled = false;
    54.             catapultLineBack.enabled = false;
    55.         }
    56.    
    57.     }
    58.  
    59.     void LineRendererSetup () {
    60.         catapultLineBack.SetPosition(0,catapultLineBack.transform.position);
    61.         catapultLineFront.SetPosition(0,catapultLineFront.transform.position);
    62.  
    63.         catapultLineBack.sortingLayerName = "foreground";
    64.         catapultLineFront.sortingLayerName = "foreground";
    65.  
    66.         catapultLineBack.sortingOrder = 1;
    67.         catapultLineFront.sortingOrder = 3;
    68.  
    69.     }
    70.  
    71.     void OnMouseDown () {
    72.         spring.enabled = false;
    73.         clickedOn = true;
    74.     }
    75.  
    76.     void OnMouseUp () {
    77.         spring.enabled = true;
    78.         Rigidbody2D.isKinematic = false;
    79.         clickedOn = false;
    80.     }
    81.  
    82.     void LineRendererupdate () {
    83.         Vector2 catapultToProjectile = transform.position - catapultLineFront.transform.position;
    84.         leftCatapultToProjectile.direction = catapultToProjectile;
    85.         Vector3 holdpoint = leftCatapultToProjectile.GetPoint(catapultToProjectile.magnitude + circlRadius);
    86.         catapultLineBack.SetPosition(1, holdpoint);
    87.         catapultLineFront.SetPosition(1, holdpoint);
    88.     }
    89.  
    90.     void Dragging () {
    91.         Vector3 mouseWorldPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    92.         Vector2 catapultToMouse = mouseWorldPoint - catapult.position;
    93.  
    94.         if(catapultToMouse.sqrMagnitude > maxStretchSqr) {
    95.             rayToMouse.direction = catapultToMouse;
    96.             mouseWorldPoint = rayToMouse.GetPoint(maxStreatch);
    97.         }
    98.  
    99.         mouseWorldPoint.z = 0f;
    100.         transform.position = mouseWorldPoint;
    101.     }
    102.  
    103. }
    104.  
    can any one please help me with the above listed 5 errors
     
  24. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Please don't reactive an old thread. Start a new one instead. It's best to post in the official Q&A thread in teaching. Navigate to the teaching section and post this in the free online training thread.

    As this is inappropriate, I will be closing this thread. I will answer in the official thread.
     
Thread Status:
Not open for further replies.