- Home /
Instantiating Prefab
So, im following this post: http://answers.unity3d.com/questions/685405/mass-place-trees-tagging.html.
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?
I'd recommend you shorten your code a bit to a $$anonymous$$imum. It's hard for anyone to read all of that.
Answer by Zedock · Aug 14, 2016 at 01:21 AM
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.
i solved this problem checking if the "tree" obj exist, if not just instantiate one.
but i have to destroy the tree now and not the object.
As you can see here:
triTree = paryTrees[intNearestPntr];
i pary the "tree" obj to the tree that is closest to the player. When the player hit the tree drop wood, and when the heath are done the "Tree" obj (Read collider obj) its destroid.
But what i rly need is that the tree that the "Collider obj" are attached get destroyed. Not the "Collider obj". As you can see here
function ApplyDammage (TheDammage : int)
{
Health -= TheDammage;
var $$anonymous$$anageScript : $$anonymous$$anageWood = GameObject.FindWithTag("Tree").GetComponent($$anonymous$$anageWood);
$$anonymous$$anageScript.Collected = true;
}
function Dead()
{
Destroy(gameObject);
}
So, where im doing Destroy(gameObject);
i have to do some Destroy(parytreeObject).
$$anonymous$$y problem is that the "paryTree" is defined in another script that is attached to another object.
So, i need this script to ins$$anonymous$$d of Destroy the "collider object" destroy the Tree that the "Collider object" is attached at.
Thats very complex to me. I have no idea what to do.
Put that aside, you comment help me a lot to make a better code. Thank very much.
Ok, i know how to instantiate a clone. But i still stuck, what i rly need i how to Destroy that Tree that the collider is attached.
Insted of Destroy(gameObject);
how can i destroy the tree that this object is attached at that momment?
thank you if you can help me or at least give a tip.
In addition to @Zedock's code to help you instantiate from a prefab, I don't see any particularly reason you couldn't copy the instantiation code again into the function Dead(), followed again with the following after you instantiate:
var $$anonymous$$anageScript : $$anonymous$$anageWood = GameObject.FindWithTag("Tree").GetComponent($$anonymous$$anageWood);
$$anonymous$$anageScript.Collected = false;
// This next bit might not be required...
$$anonymous$$anageScript.recurso = // Something along the lines of "$$anonymous$$anageWood.transform;"
I did, but the problem is that the Terrain tree remains, so the player can hit that tree forever... Im trying to destroy the Terrain Tree when "Collider Object" (that is attached to that tree) get a number of hits.
Your answer