- Home /
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[i] = (GameObject) Instantiate(fragmentPrefab, position, Quaternion.identity);
fragments[i].transform.parent = transform;
SpringJoint joint = fragments[i].GetComponent<SpringJoint>();
if (i > 0) {
joint.connectedBody = fragments[i - 1].rigidbody;
}
position += interval;
}
}
LineRenderer renderer = GetComponent<LineRenderer>();
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[i].rigidbody.position = transform.position;
fragments[i].rigidbody.isKinematic = true;
} else {
fragments[i].rigidbody.isKinematic = false;
}
}
}
void LateUpdate()
{
LineRenderer renderer = GetComponent<LineRenderer>();
for (int i = 0; i < fragments.Count; i++) {
Vector3 position = fragments[i].transform.position;
xPositions[i] = position.x;
yPositions[i] = position.y;
zPositions[i] = 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)));
}
}
}
The exception should have a stack trace. Which line is the error co$$anonymous$$g from in your script?
Answer by zharik86 · Dec 08, 2014 at 07:51 AM
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[i] = ...
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<SpringJoint>();
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<LineRenderer>();
//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[i].rigidbody.position = transform.position;
fragments[i].rigidbody.isKinematic = true;
} else {
fragments[i].rigidbody.isKinematic = false;
}
}
}
}
In LateUpdate() changing name LineRenderer too. I hope that it will help you.
Answer by Tamir1989 · Dec 08, 2014 at 08:19 AM
you set some parameters as public,(fragmentPrefab...) go to the inspector for that script, check if you have assigned the public parameters.
Tamir.