Help. (Null reference exeption)

Help. I have a script, but he not working. It says Null reference exeption at second instantiate command(string 26).
In the first time i set the instantiate variable as transform. But now it isn’t work.
Script:826190–30752–$Eggggg.js (854 Bytes)
Please, help, and tell me, why it is happening.

Game object than named “BoomSound” is null object with sound of explosition.

It could be that Unity couldn’t find any game object named “BoomSound” from the hierarchy.

Try adding an AudioSource on the game object which you apply this Egggg script on, and then play the audio:

Just a one mistake. You trying to activate sound that contaning in the Eggggg prefab. If you play audioclip in the Eggggg and then delete the Eggggg, the sound will be deleted too. That why i’am trying to instantiate command, but as you see the first instaniate what instantiating sparks is working, the second - not.

that what he printeg after 5 seconds after instantiating the Eggggg and if i trying to instantiate sound or prefab with sound:
NullReferenceException: Object reference not set to an instance of an object
Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.String cacheKeyName, System.Type[ ] cacheKeyTypes, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory)
Boo.Lang.Runtime.RuntimeServices.GetDispatcher (System.Object target, System.Object[ ] args, System.String cacheKeyName, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory)
Boo.Lang.Runtime.RuntimeServices.Invoke (System.Object target, System.String name, System.Object[ ] args)
UnityScript.Lang.UnityRuntimeServices.Invoke (System.Object target, System.String name, System.Object[ ] args, System.Type scriptBaseType)
Eggggg.Blaboom () (at Assets/Scripts/Eggggg.js:31)
Eggggg+$Wait$11+$.MoveNext () (at Assets/Scripts/Eggggg.js:15)

I just posted this in the gossip section, didn’t realize that you made a new topic :slight_smile:

Hi liver, try this instead:

var radius = 5.0;
var power = 250.0;
var sparks : GameObject;
private var boomSound : GameObject;

function Start() {
boomSound = GameObject.Find("BoomSound") as GameObject;
InvokeRepeating("Blaboom",5,5);
}

function Blaboom () {
    // Applies an explosion force to all nearby rigidbodies
    var explosionPos : Vector3 = transform.position;
    var colliders : Collider[] = Physics.OverlapSphere (explosionPos, radius);
    
    for (var hit : Collider in colliders) {
        if (!hit)
            continue;
        
        if (hit.rigidbody)
            hit.rigidbody.AddExplosionForce(power, explosionPos, radius, 1.2);
            Instantiate(sparks, transform.position, transform.rotation);
            boomSound.audio.Play();
            Destroy(hit.gameObject); 
    }
}

This should work, if you have any questions feel free to ask.

O_o wow. 25% of script that you posted i dont understand.I have corrected script before you answer that topic.
Now i have the same problem:

Null reference exeption. And of course at instantiate.

Okay let me get this straight, you have an object that you want to make a check every 5 seconds. When it does it looks for other rigidbody objects, for every object that is colliding you want to kill, and play a sound and instantiate a spark effect right?

In your original script it looks like you are trying to access an already made gameobject with an audio source, called BoomSound. is this right?

If you want i could take a look at your scene and correct any errors?

In case that you just want to instantiate a prefab that has an audio source you could do it like this:

var radius = 5.0;
var power = 250.0;
var sparks : GameObject;
var boomSound : GameObject;

function Start() {
InvokeRepeating("Blaboom",5,5);
}

function Blaboom () {
    // Applies an explosion force to all nearby rigidbodies
    var explosionPos : Vector3 = transform.position;
    var colliders : Collider[] = Physics.OverlapSphere (explosionPos, radius);
    
    for (var hit : Collider in colliders) {
        if (!hit)
            continue;
        
        if (hit.rigidbody)
            hit.rigidbody.AddExplosionForce(power, explosionPos, radius, 1.2);
            var sparkFX = Instantiate(sparks, transform.position, transform.rotation) as GameObject;
            var boomSoundFX = Instantiate(boomSound, transform.position, transform.rotation) as GameObject;
            // if the boomSound prefab audiosource doesn't have play on awake enabled then use next line, otherwise delete it
            boomSoundFX.audio.Play();
            Destroy(hit.gameObject); 
    }
}

I assume that you are remembering to assign the gameObjects that you want to instantiate in the inspector?

Script what code is means that will instantiate sparks and sounds, then destroy the object is working(i corrected it before you post your variant of code).
THAT not working now:
var EnemyBoom : Transform;
function OnCollisionEnter(collision : Collision) {
Instantiate(EnemyBoom, transform.postion, transform.rotation);
//Destroy(gameObject);
}
and again NulleReferenceExeption.

I think that’s problem with object

I don’t know why you keep insisting on using that piece of code, the variable you are declaring is of type Transform when it should be GameObject, and in regards to the Destroy - if you just put Destroy(gameObject) then it would destroy the gameobject containing the script, and therefore stop further scripts, and not the colliding object - is this what you want?

you have to do it like this:

var enemyBoom : GameObject;
function OnCollisionEnter(collision : Collision) {
Instantiate(enemyBoom, transform.position, transform.rotation);
Destroy(gameObject);
}

I know that if i call destroy command before instantiate, the last will not operate.
But, your code is not working.
UNITYPACKAGE(version 3.4.0f4, if you have an newest version, this is bad)
826373–30762–$Game.unitypackage (47.8 MB)
I know that it theeeeeeery primitive but that is all what i can for now.

The code definitely works, I think that the problem may lie in your use of it - I’m afraid that I can’t really do anything more than I already have - but good luck overcoming the issues!

It isnt work. It stuck at the instantiate string.

I think problem with objects assigning and more over where r u taking instantiate objects(means from heirarchy or project).

Maybe that problem with the object? EnemyBoom prefab, that contains only particle emiter and destroying script.

Unity package up here ^^^.

Before colliding between anything and big untextured turret:


After:

Script:
826386–30765–$EnemyHead.js (418 Bytes)
And it is not working(the selected prefab is particle emitter that don’t want to instantiate);

Ah didn’t see the package - I’ll give it a look :slight_smile: