- Home /
I have never seen a null reference exception like this one!
I have a null reference exception and this is the exact error : NullReferenceException: Object reference not set to an instance of an object (wrapper stelemref) object:stelemref (object,intptr,object) PlacingParts.Update () (at Assets/Scripts/BuildingRocket/PlacingParts.cs:39). I have a feeling it's the reason my snapping script isn't working ,but I don't know. Here is the script:
using UnityEngine;
using System.Collections;
public class PlacingParts : MonoBehaviour
{
public GameObject[] placedParts;
private int i;
private Transform currentPart;
public Transform centerPoint;
private int partLayerMask = 1 << 9;
private int snapPointLayer = 1 << 8;
private bool hasPlaced;
public float snapPointDistance = 5f;
void Start ()
{
i = 0;
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))
{
placedParts[i] = currentPart.gameObject;
hasPlaced = true;
i++;
}
}
//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))
{
}
if(placedParts != null && i < placedParts.Length)
{
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" && (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);
}
}
I could be wrong, but I think it occurs when attempting to access an element from a null array reference:
int[] someArray = null;
someArray[42];
If you double click on the error, what line does it highlight. There is nothing at line 39 that would cause this error. Is it line 34?
Yes, it's line 34, I swear it has something to do with the array!
Wait I changed the script a bit here it is again, it is line 39.
using UnityEngine;
using System.Collections;
public class PlacingParts : $$anonymous$$onoBehaviour
{
public static GameObject[] placedParts;
private int i;
private Transform currentPart;
public Transform centerPoint;
private int partLayer$$anonymous$$ask = 1 << 9;
private int snapPointLayer = 1 << 8;
private bool hasPlaced;
public float snapPointDistance = 5f;
void Start ()
{
i = 0;
hasPlaced = false;
}
void Update ()
{
//HOW TO PLACE THE ITE$$anonymous$$
if (currentPart != null && hasPlaced == false)
{
//GIVES $$anonymous$$E THE ABILITY TO $$anonymous$$OVE THE PART/OBJECT THAT WAS JUST INSTANTIATED WITH THE PLACEITE$$anonymous$$ FUNCTION
Vector3 m = Camera.main.ScreenToWorldPoint (new Vector3(Input.mousePosition.x, Input.mousePosition.y, 12.9f +( $$anonymous$$ouseOrbitImproved.distance - 13.5f)));
currentPart.position = new Vector3(m.x,m.y,m.z);
//SET THE ITE$$anonymous$$ TO HAS PLACED SO THAT IT STOPS $$anonymous$$OVING
if(Input.Get$$anonymous$$ouseButtonDown (0))
{
hasPlaced = true;
i++;
}
if(hasPlaced == true)
{
placedParts[i] = currentPart.gameObject;
}
}
//HOW TO PIC$$anonymous$$ THE OBJECT BAC$$anonymous$$ UP AFTER IT HAS BEEN PLACED
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit hit;
if(Physics.Raycast (ray, out hit, $$anonymous$$athf.Infinity, partLayer$$anonymous$$ask))
{
}
if(placedParts != null && i < placedParts.Length)
{
SnapTry(currentPart, placedParts[i].transform);
}
}
void SnapTry(Transform obj1, Transform obj2) {
//WHEN CALLING THE SCRIPT OBJ1 $$anonymous$$UST BE THE CURRENTPART
Transform[] transforms1 = obj1.GetComponentsInChildren<Transform>();
//OBJ2 HAS TO BE AN ARRAY FILLED WITH ALL THE PLACED OBJECTS TRANSFOR$$anonymous$$S
Transform[] transforms2 = obj2.GetComponentsInChildren<Transform>();
foreach(Transform tr1 in transforms1) {
foreach(Transform tr2 in transforms2) {
if (tr1.tag == "SnapPoint" && tr2.tag == "SnapPoint" && (tr1.position - tr2.position).sqr$$anonymous$$agnitude <= 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);
}
}
I'd get rid of the static modifier on the placedParts declaration. Also what are you assigning to that array in the inspector?
Answer by robertbu · May 09, 2014 at 10:04 PM
You declare the 'placedParts' variabled, but you never initialize it. That is you have a reference to an array, but you also need to declare a specific amount of space for that array. Something like:
public static GameObject[] placedParts = new GameObject[100];
That creates 100 slots in your array. If you don't know how big the array will be, you can move to a different collection type. Usually a generic List is a good choice for a variable size array. More info on collection types:
http://wiki.unity3d.com/index.php?title=Which_Kind_Of_Array_Or_Collection_Should_I_Use%3F
I am now getting an error saying that my argument is out of range. Here it is : IndexOutOfRangeException: Array index is out of range. (wrapper stelemref) object:stelemref (object,intptr,object) PlacingParts.Update () (at Assets/Scripts/BuildingRocket/PlacingParts.cs:39)
As robertbu said, us List for any array that is changing dynamically in size, not a built-in array.
While you keep incrementing i and never remove elements from the array, you will currently forever get this problem. (I cannot understand how you are trying to use an array here)
I am trying to hold all of the items I place into some type of container so I can access them all at once. At some point I will be removing them. I guess I should use a list than.