- Home /
NullReferenceException: Object reference not set to an instance of an object PlacingParts.Update () (at Assets/Scripts/BuildingRocket/PlacingParts.cs:46)
I am getting an error where I call the function SnapTry. I think it has something to do with my array that I have. Can anyone help me find out the problem? If you need me to tell you about what the script does than I can and maybe I can find a better way to achieve what I'm trying to if this won't work. Here's the script:
using UnityEngine;
using System.Collections;
public class PlacingParts : MonoBehaviour
{
public static GameObject[] placedParts;
private int i = 0;
private Transform currentPart;
public Transform centerPoint;
private int partLayerMask = 1 << 9;
private int snapPointLayer = 1 << 8;
private bool hasPlaced;
public float snapPointDistance = 0.75f;
void Start ()
{
hasPlaced = false;
}
void Update ()
{
//HOW TO PLACE THE ITEM
if (currentPart != null && hasPlaced == false)
{
//GIVES ME THE ABILITY TO MOVE THE PART/OBJECT THAT WAS JUST INSTANTIATED WITH THE PLACEITEM FUNCTION
Vector3 m = Camera.main.ScreenToWorldPoint (new Vector3(Input.mousePosition.x, Input.mousePosition.y, 12.9f +( MouseOrbitImproved.distance - 13.5f)));
currentPart.position = new Vector3(m.x,m.y,m.z);
//SET THE ITEM TO HAS PLACED SO THAT IT STOPS MOVING
if(Input.GetMouseButtonDown (0))
{
hasPlaced = true;
placedParts[i + 1] = currentPart.gameObject;
}
}
//HOW TO PICK THE OBJECT BACK UP AFTER IT HAS BEEN PLACED
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit hit;
if(Physics.Raycast (ray, out hit, Mathf.Infinity, partLayerMask))
{
}
SnapTry (currentPart, placedParts [i].transform);
}
void SnapTry(Transform obj1, Transform obj2) {
//WHEN CALLING THE SCRIPT OBJ1 MUST BE THE CURRENTPART
Transform[] transforms1 = obj1.GetComponentsInChildren<Transform>();
//OBJ2 HAS TO BE AN ARRAY FILLED WITH ALL THE PLACED OBJECTS TRANSFORMS
Transform[] transforms2 = obj2.GetComponentsInChildren<Transform>();
foreach(Transform tr1 in transforms1) {
foreach(Transform tr2 in transforms2) {
if (tr1.tag == "SnapPoint" && tr2.tag == "SnapPoint") {
if ((tr1.position - tr2.position).sqrMagnitude <= snapPointDistance) {
Debug.Log ("You could snap an object here if the script was finsihed");
}
}
}
}
}
public void PlaceItem(GameObject b)
{
hasPlaced = false;
currentPart = (((GameObject)Instantiate(b)).transform);
}
}
Answer by KellyThomas · May 09, 2014 at 04:01 AM
placedParts
is null when executing line 46:
SnapTry (currentPart, placedParts [i].transform);
You will need to initialize placedParts
or implement manual null and bounds checking.
How could I do this when placedParts starts out with 0 objects in it and none get put into it until you place an object?
That problem is the cause of the error! If it has no items then how can you access the first?
If you are happy with the logic/workflow of the rest then you can just add some sanity checks:
if(placedParts != null && i < placeParts.Length) {
SnapTry(currentPart, placedParts[i].transform);
}