I have a simple script attached to a projectile shot from a weapon. The projectile fires a raycast out in front of itself and when the raycast hits something, a bullet hole prefab is instantiated at the hit point of the collision.
What I would like to do next is add this bullet hole prefab to a generic list I have in a script (impactDecalsList) attached to a separate gameobject (impactDecalsContainer). But before I add it I need to check whether the list is bigger than a certain max length/size; if it isn’t I add it, if it is I want to use RemoveAt on the first object in the list and destroy it. Here is my script:
var impactDecalsContainer : GameObject;
var impactDecalsList : ImpactDecalsList;
var bulletHoleGeneral : GameObject;
var offsetFromSurface : float = 0.001;
var raycastDistance : float = 1;
function Start () {
impactDecalsList = impactDecalsContainer.GetComponent("ImpactDecalsList");
}
function Update () {
var hit : RaycastHit;
//Shoot a ray infront of the bullet/rocket, check it's tag on impact and instantiate the relevant decal
//Parent the decal prefab instance to the object it hit
Debug.DrawRay(transform.position, transform.forward * raycastDistance, Color.red);
if (Physics.Raycast (transform.position, transform.forward, hit, raycastDistance)) {
if (hit.transform.CompareTag ("Environment")) {
var mybullet : GameObject;
myBullet = Instantiate (bulletHoleGeneral, hit.point + (hit.normal * offsetFromSurface), Quaternion.LookRotation(hit.normal));
myBullet.transform.parent = hit.transform;
//Check the length of the list, if it's too big remove and destroy the first item in the list
//Otherwise add myBullet to the list
/*if (impactDecalsList.allImpactDecals.Count >
impactDecalsList.maxListSize) {
impactDecalsList.allImpactDecals.RemoveAt(1);
//Destroy(impactDecalsList(0));
}
else {*/
impactDecalsList.allImpactDecals.Add(myBullet);
//}
}
}
}
And here is the script (impactDecalsList) from the impactDecalsContainer gameobject:
import System.Collections.Generic;
var allImpactDecals = new List.<GameObject>();
var maxListSize : float = 40;
function Start () {
}
function Update () {
}
Unfortunately though when the projectile hits I am getting a: NullReferenceException: Object reference not set to an instance of an object, on line 28 of the code. I’ve tried referencing the other gameobject in a few different ways but I always get this same error. I’ve also read through the many Array Add/Remove questions but I haven’t found one which deals with accessing a generic list in another gameobject. Any ideas what I’m doing wrong?
Here is the Unity Package: Simple Version 8.5MB
Plenty of bullets, but the list is still at size 0. Why?

ok, your referencing .allDecalsList, which doesn't exist in component, makes sure impactDecalsList is its correct type.
– Fornoreason1000Thanks for your help, I've updated the script above and added the script of the impactDecalsList (the component on the impactDecalsContainer gameobject). I'm still getting the same error on the same line though. Is that what you meant or am I still doing something wrong?
– Hamesh81