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 jebey2 · Mar 08, 2012 at 04:30 AM · triggerontriggerenter

OnTriggerEnter questions

I am fairly new to Unity and google'd around for an answer to my problem and really didn't find anything. For reference, I am using unity for a class I am taking in which the final project is a game we have to create. I am rusty on my programming as I have not written a program in about 2 years. I have experience in Java so all my scripts will be in JavaScript. Now on to my question.

What I am currently trying to create and having problems with is an onTriggerEnter script. Imagine any adventure game you have played in a cave with icicles on the ceiling. At some point in the cave you are in, the icicles fall down at you forcing you to dodge them. I want to recreate this. To simplify this, I am using a sphere created in unity as my "icicle". I set up a plane as my "trigger point". When my character walks into this plane, the ball(icicle) will fall from its resting place above me. The problem I am having is that the script only recognizes the object it is attached to, and the object it collided with...neither of which are my ball. The way I imagine handling this(I could be wrong about my approach this is just what I was thinking), is to create my ball object(icicle) that has a rigid body attached to it. The ball(icicle) is then attached to a non rigid body object(this will allow gravity to affect it, yet it still floats in the air). My character would walk into the trigger plane, and the script would then delete the game object my ball(icicle) is attached to. This would cause the ball(icicle) to fall towards the ground where my character is at.

Anyway, as I said I am having trouble getting the script to do anything to the ball or any other object that isn't the collider or the object the script is attached to(in this instance my trigger plane). I believe is has to do with scope. As in the ball object isn't defined in the scope of the script, so it has no reference to a ball game object. How would I access and force things to happen on objects outside of the script?

Thank you in advance for any help on the subject.

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 tingham · Mar 08, 2012 at 05:14 AM 0
Share

You'll want to use a Rigidbody and simply enable "Use Gravity" when the player steps on the trigger. For the trigger, you need to ensure that the script that is handling your trigger enter event and the collider that is the trigger both exist as components at the same level in your object hierarchy. When this is the case you will be able to either GetComponent on your handler's gameObject to find other information; or just use the variables natively assigned inside of your handler behavior.

If you haven't done the platformer tutorial yet, you really should; as all of what you're trying to do here is covered in depth in that tutorial set. It's available for free on Unity's website and it can be completed in just a few hours.

All the best.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by jebey2 · Mar 08, 2012 at 04:43 PM

For the trigger, you need to ensure that the script that is handling your trigger enter event and the collider that is the trigger both exist as components at the same level in your object hierarchy. When this is the case you will be able to either GetComponent on your handler's gameObject to find other information; or just use the variables natively assigned inside of your handler behavior.

I don't understand exactly everything you mentioned here, but this is where I am having problems. I wasn't aware you can turn off gravity on a certain object and then turn it on using a script. I like that idea better than what I was planning on doing. Object hierarchy I am guessing is scope. As in, an object inside a private function cant call on an object outside of a private function and vice verse. Which would mean if the two objects are not in the same level they cant call on each other. From what I can tell they are on the same level. My testing scene I made is very simple: One plane for the ground, one plane as my tigger plane, a sphere(to act as the icicle that will fall), and a cylinder(which is my player or character). The only script I have is a script to control my characters movement. I am trying to make a second script attached to the trigger plane which will now turn on gravity on the sphere(icicle) when an object enters the trigger plane. The character will be the only thing colliding with the trigger plane, not the sphere. I know how to access information about the object colliding with the trigger, but what I want to manipulate is an object that is not colliding with the trigger plane. How would I accomplish that? Every time I try to access the sphere(icicle) it tells me it does not recognize that 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 jebey2 · Mar 08, 2012 at 04:43 PM

I figured it out! at the start of the script I created an object named target, and instantiated it like this:

target = GameObject.FindWithTag("Icicle");

I made my sphere have the tag "Icicle" and from there I just had to reference target, and BAM! I could do what I wanted. Thank you again for the useGravity suggestion. That was so much easier than what I was thinking and it worked like a charm.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

OnTriggerStay and OnTriggerEnter won't react when the player enters them. 1 Answer

Error in..."calling"(?) script 0 Answers

C# - Problem with trigger that won't activate 1 Answer

getComponent not working with Triggers? 1 Answer

Sound On Collision Not Working 3 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