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 wonkza · Apr 06, 2015 at 01:14 PM · instantiateprefabname

[C#] How can I destroy instantiated prefabs when many are created with the same name?

Hi everyone,

I'm trying to make a plant that grows in 4 stages, each stage is a prefab, my script is attached to an empty GameObject, everything is working just fine in my scene except when I want to use this GameObject multiple times at once, because then more than one instance of this script is running and they interfere with each other and causing unwanted results, I was wondering how can I have unique names for every prefab instantiated by the script?

thank you for the help, here is my script if you want to have a look.

 using UnityEngine;
 using System.Collections;
 
 public class PlantCycle : MonoBehaviour 
 
 {
     
     public float plantGrowth = 1;
     public float plantSize;
     public bool plantStage01 = true;
     public bool plantStage02 = false;
     public bool plantStage03 = false;
     public bool plantStage04 = false;
     
 
     void Start () 
 
     {
 
         Instantiate (Resources.Load ("Prefabs/Prefab_PlantStage01"), this.transform.position, this.transform.rotation); //Initialate Plant Phase 1
 
     }
 
 
 
     void Update () 
     
     {
 
 
         
         plantSize = plantSize + plantGrowth * Time.deltaTime;
 
 
         if (plantSize >= 10 && plantStage02 == false) 
 
         {
             Instantiate (Resources.Load ("Prefabs/Prefab_PlantStage02"), this.transform.position, this.transform.rotation);
 
             plantStage02 = true;
 
             Destroy (GameObject.Find("Prefab_PlantStage01(Clone)"));
 
         }
 
 
 
         else if (plantSize >= 20 && plantStage03 == false) 
 
         {
             Instantiate (Resources.Load ("Prefabs/Prefab_PlantStage03"), this.transform.position, this.transform.rotation);
             
             plantStage03 = true;
 
             Destroy (GameObject.Find("Prefab_PlantStage02(Clone)"));        
         }
 
 
 
         else if (plantSize >= 30 && plantStage04 == false) 
 
         {
             Instantiate (Resources.Load ("Prefabs/Prefab_PlantStage04"), this.transform.position, this.transform.rotation);
             
             plantStage04 = true;
 
             Destroy (GameObject.Find("Prefab_PlantStage03(Clone)"));
 
 
         }
     }
 }    



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

Answer by Digital-Phantom · Apr 06, 2015 at 01:15 PM

try making your bool variables private, not public

:)

Comment
Add comment · Show 2 · 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 Owen-Reynolds · Apr 06, 2015 at 04:46 PM 0
Share

public/private doesn't change the way variables work. You may be thinking of static vs. non-static.

avatar image Digital-Phantom · Apr 06, 2015 at 05:08 PM 0
Share

Doh, yeah was thinking static

:(

avatar image
0

Answer by Owen-Reynolds · Apr 06, 2015 at 04:53 PM

In general, you never need to search by name. If you create something, you just remember what it is, in a variable. Then, later, you can do stuff to it using that variable.

Take a look at the example pages for Instantiate. The general idea is GameObject p1=Instantiate(plant1preFab); then anytime later, talk to "your" plant1 with things like p1.position=. As a bonus, it also runs faster (not that speed matters in 99% of your code.)

Comment
Add comment · 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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

ID or name for Instantiate prefab 1 Answer

Does changing the name property of a Prefab Clone have side effects? 1 Answer

Naming Instantiated Prefabs 2 Answers

Issues with tracking prefab instances and gameobject naming (JS) 1 Answer

Assigning a prefab a number suffix at runtime. 1 Answer


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