• 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
1
Question by Preavis · Nov 26, 2011 at 02:04 AM · prefabs

Activing only one instance of a prefab

Hi guys Im pretty new to unity and am having a bit of trouble with activating a script on my prefabs. I have an enemy prefab, the enemy's control script is activated when the player collides with a trigger around the enemy. This is working fin, I enter the trigger and the enemy will chase me. The problem I'm having is with using multiple enemies, when the player enters 1 of the triggers to activate an enemy every enemy in the level activates as well.

What I'm looking for is a way to change the activate variable in the instance of the prefab that the trigger belongs without effecting any other instances of the same prefab.

Any help you can offer would be greatly appreciated

Comment
Add comment · Show 1
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 ChefZweegie · Nov 26, 2011 at 02:56 AM 0
Share

Got any code to show?

1 Reply

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

Answer by aldonaletto · Nov 26, 2011 at 03:45 AM

It seems your problem is the way you're activating the enemy script - are you using some static variable? You should post your script.
Anyway, I would do the following: create the trigger volume, then child the enemy to it, and activate its script (let's call it EnemyScript.js) in OnTriggerEnter:

function OnTriggerEnter(col: Collider){
  if (col.tag == "Player"){
    var script: EnemyScript = GetComponentInChildren(EnemyScript);
    if (script) script.enabled = true;
  }
}
This ensure that only the enemy childed to the trigger will have its script enabled.
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 Preavis · Nov 26, 2011 at 07:23 AM 0
Share

Thank you for your help, you were right I was using a static variable which is why all of the prefabs were been activated, making my enemy as a child of the trigger worked perfectly thanks.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Injecting data into Prefabs 1 Answer

Prefab Spawning With SpawnPoints (Loot System) 0 Answers

PreFabs not colliding with other GameObjecs, but collide with themselves. 0 Answers

How to fix the problem of object instantiating partially? 0 Answers

Line from Parent to Children on UI 0 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