• 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 Foxosaur · Jun 05, 2012 at 11:33 AM · colliding

Collide triggers

Hi all - thanks for the awesome and continued help with every question I raise.

I have a problem with a script I've found and am slowly modifying. The script has no errors when running but the player fails to actively collide - to the point of actually getting it to print text.

I am assuming its to do with the target variable only referring to Collider - which is nothing at this stage? Do I need to modify this to Gameobject.FirstPlayerController ?

here is my code below.

#pragma strict

var target : Collider; //this is the variable that will hold the colliding object private var triggered : boolean = false; //If we only want to detect the first time it's triggered function Start () { }

function OnTriggerEnter(collision : Collider) { if (collision != target) //The colliding object isn't our object { return; //don't do anything if it's not our target } triggered = true; print("This worked"); //If you want to have this only trigger once //Other code to do stuff on trigger goes here. You could: // 1. Increase a variable to keep track of score // 2. Transform the colliding object (send the player flying, etc) // 3. Play a sound (audio.Play() if you have an audio source) // 4. And much, much more }

Thanks.

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
1
Best Answer

Answer by aldonaletto · Jun 05, 2012 at 12:18 PM

You should drag the target object to the field Target in the Inspector - this will save a reference to its collider in the variable target. Once this has been done, the triggered variable will become true whenever the target object enters the trigger. If the trigger is static, it can recognize rigidbodies or character controllers that enter it. If the trigger moves, or if you want to detect colliders that don't have a rigidbody or character controller, you must add a kinematic rigidbody to it. OnTrigger events are sent to both, the trigger and the other object. The code above apparently is intended to be added to a trigger's script, because it looks for a specific object (the one dragged to target).

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 Foxosaur · Jun 05, 2012 at 12:23 PM 0
Share

Thank you.

I think my confusion is that some of the elements in Unity scripting is just to make the settings changable in Unity itself rather than the sheer function in the game. I had not considered that I was missing a step in Unity itself :-)

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

I have a game... 1 Answer

play animation when thrown object collides 1 Answer

Player stopping or jumping out of nowhere! 0 Answers

Simple Crate pushing.. 2 Answers

Access Violation Crash in Compiled VR game on projectile hit 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