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 HC_Juzzi · Feb 11, 2014 at 07:57 PM · prefabtriggersaccessingdestroy-clones

Run a Prefab while ONLY in the trigger zone.

Hi guys!

Been puzzled by this for the last 4 hours. In a nutshell, I only want my Particle PreFab to run while my "Player" tag is in the trigger zone. Right now, I can instantiate the Prefab "Visual" when "Player" enters the trigger zone however its trying to stop the prefab when the "player" leaves that I'm finding difficult. My current code obviously gives me "Destroying assets is not permitted"

As I'm still a bit new i'm having trouble accessing the myPart Prefab (Clone) to delete upon my "Player"'s exit trigger.

Any help is much appreciated.

public GameObject visual; void OnTriggerEnter(Collider col) { if (col.gameObject.tag == "Player") { GameObject myPart = (GameObject)Instantiate (visual); } } void OnTriggerExit(Collider col){ if (col.gameObject.tag == "Player") { Destroy(visual); } }

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by DajBuzi · Feb 11, 2014 at 08:07 PM

Just store visualClone as an empty GameObject variable:

  private GameObject visualsClone;

Then when entering the "zone" assign new instance to this variable:

  void OnTriggerEnter(Collider col){
     if(visualsClone == null) visualsClone = Instantiate(Visuals) as GameObject;
  }

And when exiting zone:

 void OnTriggerExit(Collider col){
     if(visualsClone != null) Destroy(visualsClone.gameObject);
 }
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 HC_Juzzi · Feb 11, 2014 at 08:15 PM 0
Share

Thanks very much for that DajBuzi.

At first your code destroyed the player so I added "Destroy(visualsClone);" ins$$anonymous$$d of "Destroy(col.gameObject);"

and it worked great for what i needed. $$anonymous$$uch appreciated.

avatar image DajBuzi · Feb 11, 2014 at 08:17 PM 0
Share

Yea i made mistake at first but it should be fine now.

I'm happy to hear that this helped you :)

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

19 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Check if game object is active in hierarchy from a prefab 2 Answers

Affect gameobjects with same script across network 0 Answers

Prefabs with scripts 0 Answers

Script won't destroy prefab clones when overlapping... plz help 1 Answer

Destroy an instance of a prefab 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