• 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 xToxicInferno · Feb 15, 2010 at 02:36 AM · triggersyntax

OnTriggerEnter Question

Alright this should be a fairly simple question: I have a object so when (the problem) something enters, it runs a script using OnTiggerEnter.The problem with is the fact that it runs no matter what enters. So if a stray bullet goes into it, it runs. When a enemy wonders into, it runs. I know HOW to fix it, i just don't know the syntax for it, as i am fairly new to JavaScript and Unity. Well here is the script. Also, as a bonus point, if you know about arrays, could you help me make the script better? I want to make it so i don't have to write instantiate for every single thing.

/*the variables, i would like for spawnobject to be a array but i don't how to do them */ var spawnobject : Transform; // player1 i would set as the player var player1 : Transform;

 //the function for it
 function OnTriggerEnter () {
 /* i want it so that if it was player1 

that entered it, that it will run the 'if' i dont know how to make it say that it was player 1 */ if (player1) { Instantiate(spawnobject, GameObject.Find("BallSpawn").transform.position, Quaternion.identity);

 Instantiate(spawnobject, GameObject.Find("BallSpawn1").transform.position, Quaternion.identity);

 Instantiate(spawnobject, GameObject.Find("BallSpawn2").transform.position, Quaternion.identity);

 Destroy(this,0.0);
 } 
 }

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 Sebas · Feb 15, 2010 at 02:55 AM 0
Share

I kicked out two of your tags as they were unrelated to the actual problem. Please correct the typo in your question title so that other people can find your question more easily. Thanks!

2 Replies

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

Answer by Sebas · Feb 15, 2010 at 02:43 AM

straight from the documentation:

// Destroy everything that enters the trigger
function OnTriggerEnter (other : Collider) {
Destroy(other.gameObject);
}

As you can see there, the OnTriggerEnter function accepts a collider as parameter. Basically, it tells you exactly which object entered the trigger. You can check "other" and see whether it is the player or a bullet or whatever which entered your trigger. Possibly check for the tag of your entering collider and only trigger something when the tag of your entering collider is e.g. "player"

again from the documentation:

gameObject.tag = "Player";

Combine the two script examples and you should be all set

That should result in a statement like such in your OnTriggerEnter function:

if (other.gameObject.tag == "Player") {
  //do stuff here
}
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
avatar image
0

Answer by Detinator10 · Aug 09, 2014 at 02:19 AM

I know this is already answered but you could use:

     function OnTriggerEnter (other : Collider) {
     if(other.gameObject.layer == playerlayer){
 //stuff here
 }
     }

BTW the variable player layer is a LayerMask.

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.

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

1 Person is following this question.

avatar image

Related Questions

Can't click gameobject when over another trigger? 1 Answer

Horror Game Jump Scare Help 1 Answer

Use trigger collision or position check? 1 Answer

Player detecting collision when crouching 1 Answer

Basic animation key press 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