Problem while instantiating a gameObject through C# script at runtime

I am working on a scene in which there are some windows of building and some targets hidden.Randomly windows are selected to set to open/close , when window is open randomly different targets spawn at that point. I am almost done with this part. But I have a problem in instantiating a prefab (GameObjects of target) through script, Like there is an example of my script

Random_Enemies.cs:

if(window is down) // pseudo code 

Instantiate(Enemy_Arr[(Random.Range(minE,maxE))],spawn1.transform.position,Quaternion.AngleAxis(180, Vector3.down));

Damager-receiver.js:

if(got hit)// pseudo code 
Destroy(gameObject);
    Instantiate(Coin, transform.position,transform.rotation);

Random_Enemies is a C# script that is spawning enemies at random when window is down, and Damage-Receiver scripts check if Ray cast hit the “target” tag, and instantiate a Coin in replacement to that object. But its not working ? Coin doesn’t appear what I am missing , is it not possible to instantiate a prefab at runtime and then again instantiate another prefab at that position. Any help would be appreciated ?

Here are the script files, Enemies_Spawn.cs is spawning enemy at random on specified position , its working Fine. Then DamageReceiver.js file is calling another prefab in replacement of the enemy. And here is the problem it doesnt instantiate at that position. Why is that so ?

Enemies_Spawn.cs:

IEnumerator Start()
{
    while (true) {
			Enemies_Spawn();
			
           yield return new WaitForSeconds(Random.Range(minT, maxT)); // wait X times for opening next window
    		}
}	


    void Enemies_Spawn()
    {
    		targetSelect = Random.Range(minE,maxE);		
    	
    			
    switch(targetSelect)
    		{ // switch start
    			
    			
    case 1: // for  gameobject window 1
    		{
    			 
    	#region 
    		// spawn enemies here at the position of Spawn gameobjects
    		Instantiate(Enemy_Arr[(Random.Range(minE,maxE))],spawn1.transform.position,Quaternion.AngleAxis(180, Vector3.down));
    				
    
    			}
    	#endregion
    				break;

DamageReciever.js

#pragma strict
var hitPoints = 5.0;
var detonationDelay = 0.0;
var Sparkles : Transform;
var Coin : Rigidbody;

public static var score: int = 0;

function ApplyDamage (damage : float) {
	// We already have less than 0 hitpoints, maybe we got killed already?
	if (hitPoints <= 0.0)
		return;
		
	hitPoints -= damage;
	if (hitPoints <= 0.0) {
		
		score = score + 5;
		Invoke("DelayedDetonate", detonationDelay);
	}
}

function DelayedDetonate () {
	BroadcastMessage ("Detonate");
}

function Detonate () {
	

	
	// save position of current target 
	
	transform.position = new Vector3(gameObject.transform.position.x,gameObject.transform.position.y,gameObject.transform.position.z);
	transform.rotation = gameObject.transform.rotation;
	
	Shooting.Enemy_dead +=1;
	// Destroy ourselves
	Destroy(gameObject);
	
				
	if (Sparkles)
	{
	
		Instantiate(Sparkles, transform.position,transform.rotation);
	}
	if (Coin) 
	
	{
		Instantiate(Coin, transform.position,transform.rotation);

	}
	
}

// We require the barrel to be a rigidbody, so that it can do nice physics
@script RequireComponent (Rigidbody

)

It appears that the Coin prefab is not initialised. I’m also not sure what you are doing with the position and rotation in there, they appear to be setting it back to the same value…

 function Detonate () {
	

	

	
	Shooting.Enemy_dead +=1;
	
	
				
	if (Sparkles)
	{
	
		Instantiate(Sparkles, transform.position,transform.rotation);
	}
	if (Coin) 
	
	{
		Instantiate(Coin, transform.position,transform.rotation);

	}

    // Destroy ourselves
	Destroy(gameObject);
}

// Destroy ourselves
Destroy(gameObject);

    if (Sparkles)
    {

       Instantiate(Sparkles, transform.position,transform.rotation);
    }
    if (Coin) 

    {
       Instantiate(Coin, transform.position,transform.rotation);

    }

It looks like you are instantiating the coin after you have destroyed the object that is spawning it, you mind find it can either no longer spawn the coin because the script has been deleted along with the gameobject, or that it can’t get the game objects position/rotation for the same reason.

I could be wrong of course, I have a feeling it would throw an exception if this was the problem.