Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 sketchers1 · Aug 20, 2011 at 08:36 PM · collisionontriggerenterontriggerexitactivation

How to Apply a script During collison?

So, I have a car game, and i have a tornado in that car game. I also have a randomflying script. What I want is to have the randomflying script to be applied to the car while the car is touching the tornado, and to have the script deactivated when the car is not touching the tornado. I know that I would Probably have to Use OnTriggerEnter..... but im not really good at js. can anyone give me an accurate script or any tips?

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

Answer by J.D. · Aug 20, 2011 at 10:08 PM

There is a lot of ways to do that in Unity.

I would use this:

Tag your Tornado as "Tornado".

The OnTriggerEnter function has a parameter that you have to use to check what is the collider object.

(I use C#, but just look at the LOGIC, and not the syntax)

 //This should be attached to your car
 void OnCollisionStay(Collision obj){
   //obj will be the Tornado, right?
 if(obj.gameobject.tag == "Tornado"){
  YouCarFlyAwayHere();
   }
 }


You could also use OnCollisionEnter(). But that way you maybe should implement OnCollisionExit() as well.

I hope I helped.

Good Luck.

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 diddykonga · Aug 21, 2011 at 05:50 AM

Here try this.

 void OnTriggerEnter (Collider info) {
     if(info.gameObject.tag == "Car")
     {
         info.gameObject.AddComponent("RandomFlying");
     }
 }

 void OnTriggerExit (Collider info) {
     if(info.gameObject.tag == "Car")
     {
         info.gameObject.GetComponent<RandomFlying>().Destroy();
     }
 }

There we go, now all you need to do, is to set your tornado's collider to isTrigger. Then give your car, the Car tag, and your all set :)

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 sketchers1 · Aug 21, 2011 at 05:54 AM 0
Share

I tried this but it gave me tons of errors that i couldnt figure out how to fix. :(

avatar image
0

Answer by sketchers1 · Aug 25, 2011 at 02:55 AM

So, I tagged the tornado as Player, and here is the script I used:

function Update () {

//This should be attached to your car

void; OnCollisionStay("Tornado");{

//obj will be the Tornado, right?

if(obj.gameobject.tag == "Player");{

info.gameObject.AddComponent("RandomFlying");

}

}

}

This gave me these errors:

Assets/Scripts/TornadoScript.js(6,1): BCE0043: Unexpected token: if.

Assets/Scripts/TornadoScript.js(7,46): BCE0044: expecting :, found ';'.

ummmmm these are the 2 errors I cant figure out??? any help?

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 J.D. · Aug 26, 2011 at 06:04 PM 0
Share

As I said, this is just the Logic. And is in C#. Your script has .js extention.

Just Understand what i meant in code, and write in Javascript. Its really easy, if you read carefully.

Good luck!

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Why would OnTriggerEnter get called, but not OnTriggerExit? 0 Answers

Problem with OnTriggerExit 1 Answer

Weird OnTriggerEnter / Exit behavior 1 Answer

OnTriggerExit doesn't work when scaling collider 0 Answers

Why is OnTriggerEnter2D executing multiple times before I get an OnTriggerExit2D? 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