C# Need Help Finding Where Null Reference Exception Is

I have a script that generates a rope down the y axis. For some reason I’m getting a null reference exception and I can’t figure out where it’s coming from. When I click on it sends me to random lines on my code. Any help would be appreciated.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class RopeController : MonoBehaviour
{
	public GameObject fragmentPrefab;
	
	public  List<GameObject> fragments = new List<GameObject>();
	private Vector3 interval = new Vector3(0f, -0.5f, 0f);

	private float[] xPositions;
	private float[] yPositions;
	private float[] zPositions;
	
	private CatmullRomSpline splineX;
	private CatmullRomSpline splineY;
	private CatmullRomSpline splineZ;
	
	private int splineFactor = 4;
	
	void Start()
	{

	}
	
	void Update()
	{
		Vector3 position = transform.position;
		
		for (int i = 0; i < (int)gameObject.transform.position.y; i++) {
		if(fragments.Count <= (int)gameObject.transform.position.y){
			fragments.Add(null);
			fragments *= (GameObject) Instantiate(fragmentPrefab, position, Quaternion.identity);*

_ fragments*.transform.parent = transform;*_

_ SpringJoint joint = fragments*.GetComponent();
if (i > 0) {
joint.connectedBody = fragments[i - 1].rigidbody;
}*_

* position += interval;*
* }*
* }*

* LineRenderer renderer = GetComponent();*
_ renderer.SetVertexCount((fragments.Count - 1) * splineFactor + 1);_

* xPositions = new float[fragments.Count];*
* yPositions = new float[fragments.Count];*
* zPositions = new float[fragments.Count];*

* splineX = new CatmullRomSpline(xPositions);*
* splineY = new CatmullRomSpline(yPositions);*
* splineZ = new CatmullRomSpline(zPositions);*

* float activeFragmentNum = (float)fragments.Count;*
_ float vy = Input.GetAxisRaw(“Vertical”) * 20f * Time.deltaTime;
* activeFragmentNum = Mathf.Clamp(activeFragmentNum + vy, 0, fragments.Count);*_

* for (int i = 0; i < fragments.Count; i++) {*
* if (i <= fragments.Count - activeFragmentNum) {*
_ fragments*.rigidbody.position = transform.position;
fragments.rigidbody.isKinematic = true;
} else {
fragments.rigidbody.isKinematic = false;
}
}
}*_

* void LateUpdate()*
* {*
* LineRenderer renderer = GetComponent();*

* for (int i = 0; i < fragments.Count; i++) {*
_ Vector3 position = fragments*.transform.position;
xPositions = position.x;
yPositions = position.y;
zPositions = position.z;
}*_

_ for (int i = 0; i < (fragments.Count - 1) * splineFactor + 1; i++) {
* renderer.SetPosition(i, new Vector3(
splineX.GetValue(i / (float) splineFactor),
splineY.GetValue(i / (float) splineFactor),
splineZ.GetValue(i / (float) splineFactor)));
}
}
}*_

you set some parameters as public,(fragmentPrefab…)
go to the inspector for that script, check if you have assigned the public parameters.

Tamir.

You have error, because your logic in loop “for” is not correct. You write:

 for (int i = 0; i < (int)gameObject.transform.position.y; i++) {
  if(fragments.Count <= (int)gameObject.transform.position.y){
   fragments.Add(null);
    fragments *= ...*

For, example, List of fragments is not empty (for example, have 5 elements). Then in condition “if” (for i == 0) you add null object. Last object have index 5, but you work with index 0. I think, you must modify last element of List. Try this:
void Update() {
Vector3 position = transform.position;

for (int i = 0; i < (int)gameObject.transform.position.y; i++) {
if(fragments.Count <= (int)gameObject.transform.position.y){
fragments.Add(null);
//Use not “i”, but last index
fragments[fragments.Count-1] = (GameObject) Instantiate(fragmentPrefab, position, Quaternion.identity);
fragments[fragments.Count-1].transform.parent = transform;

SpringJoint joint = fragments[fragments.Count-1].GetComponent();
if (fragments.Count-1 > 0) {
//Connect to prelast element
joint.connectedBody = fragments[fragments.Count - 2].rigidbody;
}

position += interval;
}
}
//Not use names matches with method, change “renderer” on “myrenderer”
LineRenderer myrenderer = GetComponent();
//Check counts:
if(fragments.Count > 0) {
myrenderer.SetVertexCount((fragments.Count - 1) * splineFactor + 1);
//Not correct index, for example list have 7 elements (Count), but last index is 6
xPositions = new float[fragments.Count - 1];
yPositions = new float[fragments.Count - 1];
zPositions = new float[fragments.Count - 1];

splineX = new CatmullRomSpline(xPositions);
splineY = new CatmullRomSpline(yPositions);
splineZ = new CatmullRomSpline(zPositions);

float activeFragmentNum = (float)fragments.Count;
float vy = Input.GetAxisRaw(“Vertical”) * 20f * Time.deltaTime;
activeFragmentNum = Mathf.Clamp(activeFragmentNum + vy, 0, fragments.Count);

for (int i = 0; i < fragments.Count; i++) {
if (i <= fragments.Count - activeFragmentNum) {
fragments*.rigidbody.position = transform.position;*
fragments*.rigidbody.isKinematic = true;*
} else {
fragments*.rigidbody.isKinematic = false;*
}
}
}
}
In LateUpdate() changing name LineRenderer too. I hope that it will help you.