Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by defterniko · Nov 07, 2012 at 02:46 PM · c#gameobjecttransformdestroy

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image defterniko · Nov 07, 2012 at 04:03 PM 0
Share

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

avatar image
0

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image defterniko · Nov 07, 2012 at 03:16 PM 0
Share

Thanks i'll give that a shot sounds like it could work.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Destroying an object after its out of sight ! 2 Answers

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

Destroy a Gameobject with a UI Button 3 Answers

First child of a gameobject 3 Answers

The name "Destory" Does not exist in the current context 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges