I want to increase my ammo count when my missles are destoryed
ammoCount is on the launcher script and destory is on the missle script - so I want to increase ammoCount from my missle script
I have got a bit lost but I was trying
Missle.js
var addAmmo:Launcher;
function OnCollisionEnter(collision : Collision) {
..../etc
Destroy (gameObject);
addAmmo.ammoCount + 1;
but it complains about NullReferenceException
Launcher.js
public var ammoCount = 4;
function Update () {
if (...etc..
instantiatedProjectile.velocity = transform.forward * speed;
ammoCount --;
The reference to instantiatedProjectile in your Launcher script might point to null if the instantiatedProjectile has already been destroyed. I can’t tell for sure if this would ever happen, though.
Also, I notice that you’re calling Destroy in your Missile script before you add to your ammo count. This means that the ammoCount line won’t ever get called.
The problem is probably that the missile var addAmmo:Launcher; is set to be something in a scene and then the missile is assigned to a prefab and instantiated. This will break that link. In your missile instantiator you need missile.addAmmo = this;
No. Destroy does not take effect until the next frame.
Although I forgot to say that static variables have the feature of retaining their value from one run to the next. So you probably want to assign the value of ammoCount in a Start function…just declaring it at the top of the script isn’t enough.