• 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 /
This question was closed Sep 07, 2018 at 06:22 PM by MSavioti for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by MSavioti · Sep 07, 2018 at 08:29 AM · triggergetcomponent

Trouble when getting object reference with GetComponent by using OnTriggerEnter2D

I saw and read about three related topics but none had a answer to my problem.

The thing is, I'm creating a ladder script for a 2D side scrolling game and the idea is disable the gravity and the "x" movement when the player press "up" nead the ladder to climb it. The problem is that I need a collider on the upper part so the player can walk on the upper floor without falling. I pointed that upper part on the following image:

alt text

So the idea is to disable the collider when I'm climbing the ladder and enable it when I'm not. And that collider is on the ladder's script, so I created a variable of type of the LadderScript on the player's script to receive the reference of the ladder that the player is about to climb when the player enters the trigger area:

 private void OnTriggerEnter2D(Collider2D collision) {
         if (collision.gameObject.layer == 14) {
             isInsideLadderArea = true; //this works
             lastLadderPosition = new Vector3(collision.transform.position.x, collision.transform.position.y); //this works
             lastLadder = collision.gameObject.GetComponent<LadderScript>(); // this returns null (why???)
             print(collision.gameObject.layer); //for debug purposes, prints exactly the layer that the object it's on
         }
     }
 
 private void DropFromLadder() {
         isClimbingLadder = false;
         rb2d.gravityScale = 4;
         lastLadder.EnableCollider(); //this get me an error because of of null reference
     }
 
     private void ClimbLadder() {
         isClimbingLadder = true;
         rb2d.gravityScale = 0;
         lastLadder.DisableCollider(); //this get me an error because of of null reference
     }


Then when the OnTriggerEnter2D is activated, I get a vector 3 position of the object, a change on my bool value, I can even print the layer that my object is on, but some reason that completely eludes me, I can't set the reference of the ladder object on my variable.

On execution time, I forcedly dragged my ladder object into the "lastLadder" field and it works fine, but if exit the trigger area, as soon as enter the trigger area again, my lastLadder gets a null value again.

I tried everything, including static variables, methods with parameters but got nothing, so I'd be very, very grateful if anyone can help me.

This the ladder script: public class LadderScript : MonoBehaviour {

     public Collider2D ladderUpperGround;
 
     void Start () {
         ladderUpperGround.enabled = true;
     }
 
     private void OnTriggerEnter2D(Collider2D collision) {
         if (collision.gameObject.layer == 15) {
             print("player entered"); //not activating
         }
     }
 
     public void EnableCollider() {
         ladderUpperGround.enabled = true;
     }
 
     public void DisableCollider() {
         ladderUpperGround.enabled = false;
     }
 }
screen-shot-09-07-18-at-0439-am.jpg (61.5 kB)
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

  • Sort: 
avatar image
1
Best Answer

Answer by misher · Sep 07, 2018 at 09:21 AM

Are you sure that your trigger collider for a ladder is attached to the same gameobject as your LadderScript component? If it is not, then you should use GetComponentInChildren or GetComponentInParent instead of just GetComponent in base of where is LadderScript attached to the ladder in scene/prefab hierarchy.

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 MSavioti · Sep 07, 2018 at 01:06 PM 0
Share

The LadderScript is the parent of the object with the trigger and your tip about GetComponentInParent totally nailed it. Thank you very much!

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

108 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image

Related Questions

GetComponent().Play(); Question(s) 2 Answers

Changing the texture of another GameObject when pressing E 2 Answers

Trigger not getting GameObject 2 Answers

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

How Do I get the component of individual objects with same tag using On Trigger Enter. 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