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 ITGeist · Dec 09, 2012 at 01:46 AM · triggerdetect

Detect with Object has been triggered

Hello there,

I'd like to check which Object got triggered by the player, but nothing happens with my script.

 void OnTriggerEnter(GameObject touched)
     {
         if(touched.name == "Trigger_Start")
         {
             startTime = Time.time;
         }
         if(touched.name == "Trigger_Finish")
         {
             finished = true;
             startTime = 0;
         }
     }

Anyone knows how to do it?

Greetz, ITGeist.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by azmat786n · Dec 09, 2012 at 04:41 AM

 //if not work change OnTriggerEnter-->OnTriggerStay
 void OnTriggerEnter(GameObject touched)
     {
        if(touched.name == "Trigger_Start")
        {
          Timer += Time.deltaTime;
      startTime = Mathf.RoundToInt(Timer);
        }
        if(touched.name == "Trigger_Finish")
        {
          finished = true;
          startTime = 0;
        }
     }
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 ITGeist · Dec 09, 2012 at 09:54 AM 0
Share

Sorry, didn't work. :(

avatar image azmat786n · Dec 09, 2012 at 01:31 PM 0
Share

you need to make 2 variables with name of

var Timer:float; var startTime:int;

avatar image
0

Answer by devain · Dec 09, 2012 at 11:23 AM

Using a string compare is quite expensive. I would suggest you compare by reference instead, something like this:

     void OnTriggerEnter(Collider other) 
     {
         ObjectToLookFor reference = other.GetComponent<ObjectToLookFor>();
         if ( reference != null )
         {
             // Do fancy stuff, like comparing the name - if you really really have to
         }
         else
         {
             // Do stuff that didn't require this type of object
         }
     }
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 SinPin · Dec 09, 2012 at 11:22 AM

Hey ITGeist ;)

You should know some details about the trigger system:

1st: The Prototype of OnTriggerEnter is not "void OnTriggerEnter(GameObject touched)", but " void OnTriggerEnter(Collider other)" as you can read here

2nd: There are some things, you have to do in the Unity Editor...

All your Objects, you want to use as trigger, should have a Rigidbody (Component -> Physics -> Rigidbody) - Set the "Use Gravity" flag to false (unticked) if you do not want it to act as a normal rigidbody.

And all your objects should have a collider (e.g. BoxCollider) (Comes automatically with the standard objects, if you don't have it: Component -> Physics -> e.g. Box Collider) where the "is Trigger" Flag is set to true (ticked). (Actually only one of the colliding objects must have set the Trigger-Flag to true, but every object needs to have a collider.)

I hope, this helps you ;)

Yours, SinPin

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

13 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

Related Questions

Play parent animation without affecting the child animation 1 Answer

Trigger Script Not Firing 4 Answers

how to make triger fireball not to pass through spesific objects 2 Answers

How to trigger a Script from an Object. 0 Answers

Triggered box works automatically for OnMouseEnter for objects inside. 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