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)));
}
}
}*_