Instantiating Prefab

So, im following this post: Mass Place Trees tagging - Questions & Answers - Unity Discussions.

And all working good. But i want to chance something, and i need some help. Im very newbie on this.

#pragma strict
 
 public var player : Transform;
  
 private var paryTrees : TreeInstance[];
 private var pvecTerrainPosition : Vector3;
 private var pvecTerrainSize : Vector3;
 private var pgobTreeCollide : GameObject;
 private var pvecCollideScale : Vector3;
 private var pbooCollideWithTrees : boolean = false;
  
 function Start()
 {
     // Get the terrain's position
     pvecTerrainPosition = Terrain.activeTerrain.transform.position;
      
     // Get the terrain's size from the terrain data
     pvecTerrainSize = Terrain.activeTerrain.terrainData.size;
     // Get the tree instances
     paryTrees = Terrain.activeTerrain.terrainData.treeInstances;
      
     // Get the invisible capsule having the capsule collider that makes the nearest tree solid
     pgobTreeCollide = GameObject.Find("Tree"); // This is a capsule having a capsule collider, but when the flier hits it we want it to be reported that the flier hit a tree.
      
     // Are there trees and a tree collider?
     if ((pgobTreeCollide != null) && (paryTrees.length > 0))
     {
         // Set a flag to make this script useful
         pbooCollideWithTrees = true;
         // Get the original local scale of the capsule. This is manually matched to the scale of the prototype of the tree.
         pvecCollideScale = pgobTreeCollide.transform.localScale;
     }
     // No need to use this script
     else 
     {
         Debug.LogWarning( "NO CAPSULE NAMED TREE FOUND, OR NO TERRAIN TREES, DESTROYING SCRIPT..." );
         Destroy(this);
     }
     
     // has the player been assigned in the Inspector?
     if ( !player ) 
     {
         Debug.LogWarning( "NO PLAYER OBJECT IN THE INSPECTOR, DESTROYING SCRIPT..." );
         Destroy(this);
     }
 }
  
 function Update() 
 {
     var L : int;
     var triTree : TreeInstance;
     
     //var vecFlier : Vector3 = sctFly.svecXYZ; // My protagonist's position, passed by a static variable in a script called sctFly.
     var vecFlier : Vector3 = player.position; // using the player transform, dropped in the inspector
     
     var fltProximity : float;
     var fltNearest : float = 9999.9999; // Farther, to start, than is possible in my game.
     var vec3 : Vector3;
     var vecTree : Vector3;
     var intNearestPntr : int;
      
     // Test the flag
     if (pbooCollideWithTrees == true)
     {
         // Find the nearest tree to the flier
         for (L = 0; L < paryTrees.length; L++)
         {
             // Get the tree instance
             triTree = paryTrees[L];
             // Get the normalized tree position
             vecTree = triTree.position;
             // Get the world coordinates of the tree position
             vec3 = (Vector3.Scale(pvecTerrainSize, vecTree) + pvecTerrainPosition);
             // Calculate the proximity
             fltProximity = Vector3.Distance(vecFlier, vec3);
             // Nearest so far?
             if (fltProximity < fltNearest)
             {
                 // Remember the nearest
                 fltNearest = fltProximity;
                 // Remember the index
                 intNearestPntr = L;
             }
         }
         // Get the closest tree
         triTree = paryTrees[intNearestPntr];
         // Get the normalized tree position of the closest tree
         vecTree = triTree.position;
         // Get the world coordinates of the tree position
         vec3 = (Vector3.Scale(pvecTerrainSize, vecTree) + pvecTerrainPosition);
         // Scale the capsule having the capsule collider that represents a solid tree
         pgobTreeCollide.transform.localScale = (pvecCollideScale * triTree.heightScale);
         // Add some height to position the capsule correctly on the tree
         vec3.y += pgobTreeCollide.transform.localScale.y;
         // Position the capsule having the capsule collider at the nearest tree
         pgobTreeCollide.transform.position = vec3;
     }
 }

on the object “Tree” im using this:

#pragma strict

var Collected : boolean = false;

private var showGui : boolean = false;

var recurso : Transform;
var player : Transform;

function Update()
{

    if(Collected == true)
    {
        MakeRecurso();
    }
}

function MakeRecurso()
{
    Instantiate (recurso, player.transform.position, Quaternion.identity);
    Collected = false;
    showGui = false;
}

and this:

#pragma strict

var Health = 100;

function Update ()
{
    if(Health <= 0)
    {
		
        Dead();
		
    } 
}

function ApplyDammage (TheDammage : int)
    {
        Health -= TheDammage;
        var ManageScript : ManageWood = GameObject.FindWithTag("Tree").GetComponent(ManageWood);	        
        ManageScript.Collected = true;
	
    }

    function Dead()
    {
	
        Destroy(gameObject);
	
	
    }

Everything works great, no problems. When I approach a tree the object move to that tree and I hit the object and it will drop logs. Perfect!

But as you can see this object has a life stats. And it disappears when it is destroyed.

I need to modify my script so that this object is instantiated from a Prefab. When the script detects that the object is destroyed it will instantiate a new one.

Can anyone ELI5 me please?

So the only point in which you are instantiating anything is on the Tree script - line 21.

You currently have: `Instantiate (recurso, player.transform.position, Quaternion.identity);

I don't know if this would fix you problem, but I would start by assigning the Instantiate function to a variable. Like so (note I do not work with javascript so the syntax for the variable may be wrong): var recursoObject : GameObject = Instantiate (recurso, player.transform.position, Quaternion.identity) as GameObject;`

This will make it easier to create an delete any prefab.

If this is not what you are looking for, sorry for this but please note it was unclear about which section to look for in your massive paste of code. If you still need help I would begin by only providing the absolute necessary code.