- Home /
destroying game object in a specific transform
Im currently developing a farm simulation style game and have the growing of crops stages working. Now though i need to implement the 'harvest' stage and have it do the growing stages but in reverse. There are three stages to the plant model giving three stages of growth.
Therefore what ive literally done is the reverse in code and it works when you only have one plot of land with that specific crop. However the user player has 9 plots and can grow a choice of three plants.
When more than one of the same crop is grown, when i choose to harvest a crop for the plot i am currently at, the game doesn't always choose that one, it will look for the first of say plantThreeStageThree(clone).
if(harvest == true)
{
if (timeTilBuilt > 0)
{
if (PlantOneBool == true)
timeTilBuilt -= 1 * Time.deltaTime;
if (PlantTwoBool == true)
timeTilBuilt -= 1 * Time.deltaTime;
if (PlantThreeBool == true)
timeTilBuilt -= 1 * Time.deltaTime;
}
if (timeTilBuilt >= timeSet / 2)
{
if (checkPlantStageOneHarvest == false)
{
if (PlantOneBool == true)
{
Destroy(GameObject.Find("PlantOneStageThree(Clone)"));
Plant = (Transform)Instantiate(PlantOneStageTwo, new Vector3(transform.position.x + 2.5f,transform.position.y,transform.position.z), transform.rotation);
checkPlantStageOneHarvest = true;
}
if (PlantTwoBool == true)
{
Destroy(GameObject.Find("PlantTwoStageThree(Clone)"));
Plant = (Transform)Instantiate(PlantTwoStageTwo, new Vector3(transform.position.x + 2.5f,transform.position.y,transform.position.z), transform.rotation);
checkPlantStageOneHarvest = true;
}
if (PlantThreeBool == true)
{
Destroy(GameObject.Find("PlantThreeStageThree(Clone)"));
Plant = (Transform)Instantiate(PlantThreeStageTwo, new Vector3(transform.position.x + 2.5f,transform.position.y,transform.position.z), transform.rotation);
checkPlantStageOneHarvest = true;
}
}
}
if (timeTilBuilt <= timeSet / 2)
{
if (checkPlantStageTwoHarvest == false)
{
if (PlantOneBool == true)
{
Destroy(GameObject.Find("PlantOneStageTwo(Clone)"));
Plant = (Transform)Instantiate(PlantOneStageOne, new Vector3(transform.position.x + 2.5f,transform.position.y,transform.position.z), transform.rotation);
checkPlantStageTwoHarvest = true;
}
if (PlantTwoBool == true)
{
Destroy(GameObject.Find("PlantTwoStageTwo(Clone)"));
Plant = (Transform)Instantiate(PlantTwoStageOne, new Vector3(transform.position.x + 2.5f,transform.position.y,transform.position.z), transform.rotation);
checkPlantStageTwoHarvest = true;
}
if (PlantThreeBool == true)
{
Destroy(GameObject.Find("PlantThreeStageTwo(Clone)"));
Plant = (Transform)Instantiate(PlantThreeStageOne, new Vector3(transform.position.x + 2.5f,transform.position.y,transform.position.z), transform.rotation);
checkPlantStageTwoHarvest = true;
}
}
}
if (timeTilBuilt <= 0)
{
timeTilBuilt = 0;
if (checkPlantHarvest == false)
{
if (PlantOneBool == true)
{
Destroy(GameObject.Find("PlantOneStageOne(Clone)"));
i = 0;
amount = 10;
checkPlantHarvest = true;
PlantOneBool = false;
}
if (PlantTwoBool == true)
{
Destroy(GameObject.Find("PlantTwoStageOne(Clone)"));
i = 1;
amount = 5;
checkPlantHarvest = true;
PlantTwoBool = false;
}
if (PlantThreeBool == true)
{
Destroy(GameObject.Find("PlantThreeStageOne(Clone)"));
i = 2;
amount = 2;
checkPlantHarvest = true;
PlantThreeBool = false;
}
checkPlantStageOne = false;
checkPlantStageTwo = false;
checkPlantBuilt = false;
hasPlant = false;
harvest = false;
}
}
}
Heres the code i have at the moment and have been wrecking my brain for hours but cannot for the life of me figure it out.
Where it says ` Destroy(GameObject.Find("PlantTwoStageOne(Clone)"));` I thought of changing that too ` Destroy(transform.Find("PlantTwoStageOne(Clone)"));` or ` transform.Destroy(GameObject.Find("PlantTwoStageOne(Clone)"));` However these don't work either.
I have also noticed that it will take the "PlantOneStageThree(Clone)" from a near by plot, move it to the plot i am currently harvesting and delete the original, leaving the moved plant in that plot and the plot where it has been moved from, thinking it is still there.
Any ideas folks?
Answer by coastwise · Nov 07, 2012 at 03:24 PM
Why not instead of storing a bool for each plant, store a reference to the actual GameObject (this could be of type Transform, GameObject, or better yet whatever component script that you use to make a plant a plant).
This way you don't need to call `Find` at all and you can just `Destroy` the plant directly.
You can check to see if a plant has been planted by comparing the reference to `null`. You can see if it is a specific kind of plant by checking the result of GetComponent(PlantType).
To answer your question more thoroughly however, we'd need to know more about how this currently works. What is this script attached to (the plot i assume)? Can the user grow multiple plants in a single plot (are the 'one', 'two', 'three's the kind of plant, or a number of plants)?
If this is a script attached to the plot, and a plot can have a single plant planted in it, I think it would look something like so:
// it's harvest time and we know there is a plant planted here
if (harvest && plant != null) {
if (timeTilBuilt > 0) {
timeTilBuilt -= 1 * Time.deltaTime;
}
if (timeTilBuilt <= 0) {
// i'm not sure what these values are for,
// but they should probably be defined in the plant script
// that way we can just get the values we need without having to lookup the type
i = plant.i;
amount = plant.amount;
// and we simply destroy the plant
Destroy(plant);
}
}
I don't really understand why you need to "grow in reverse", so I've left that part out. Please feel free to elaborate.
Hopefully this code gives you a peek at a cleaner approach, and points you in the right direction at least.
i "grow in reverse" becuase each plant has three stages of growth with each stage having a different 3D model representing it. So when i'm harvesting the need to be " chopped down" which is the 3 models in reverse
Answer by dorpeleg · Nov 07, 2012 at 03:10 PM
There is one way I was thinking about.
I'm only going to give you the general concept.
When using Instantiate, the function automatically adds "(clone)" to the original name.
And because all of your objects have the same name, you can't use the Find function.
So how about this: right after planting the plant use Find to get it then change its name to "original name + i" where i is a var that is increasing every new plant.
Then when you harvest, you can get the plants name which will be original.
That's just one idea. See if it works out for you
Thanks i'll give that a shot sounds like it could work.