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 $$anonymous$$ · Aug 23, 2016 at 07:41 AM · c#physicsplatformer

How do I change the properties of a Component in a Parent Object?

Hello. I am making a platform where the player can jump through it if they are approaching it from the bottom. I have the parent which is a sprite with a normal Box Collider 2D that the player collides with and a child that is a trigger. When the player enters the trigger I want the Box Collider of the parent to become a trigger so the player is able to jump through it. Here is my script:` BoxCollider2D boxCol;

 void Start()
 {
     boxCol = this.gameObject.GetComponentInParent(typeof(BoxCollider2D)) as BoxCollider2D;
 }


 void OnTriggerEnter2D(Collider2D other)
 {
     boxCol.isTrigger = true;
     Debug.Log(boxCol.isTrigger);
     
 }

 void OnTriggerExit2D(Collider2D other)
 {
     boxCol.isTrigger = false;
     Debug.Log(boxCol.isTrigger);
 }


I think it is a problem when I try to reference the Box Collider 2D Component of the parent Game Object. Any idea on how I can reference and change this component properly is greatly appreciated. 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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by $$anonymous$$ · Aug 24, 2016 at 08:16 AM

Ah! I had to reference the GameObject directly.

 BoxCollider2D boxCol;
 public GameObject platform;

 void Start()
 {
     boxCol = platform.GetComponent(typeof(BoxCollider2D)) as BoxCollider2D;
 
 }


 void OnTriggerEnter2D(Collider2D other)
 {
     boxCol.isTrigger = true;
     Debug.Log(boxCol.isTrigger);
     
 }

 void OnTriggerExit2D(Collider2D other)
 {
     boxCol.isTrigger = false;
     Debug.Log(boxCol.isTrigger);
 }

}

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 Bieere · Aug 23, 2016 at 07:05 PM

So the issue isnt actually with the component setting, the issue you're seeing is that you're attempting to set the trigger to true when it enters the trigger, which wont work.

You can rewrite that line as:

  boxCol = this.gameObject.GetComponentInParent<BoxCollider2D>() as BoxCollider2D;

It needs to be set to trigger = true before it enters that collider, so you can either use the OnCollisionEnter2D() function, or add to your script, either on the object you're attempting to pass through, or the character, that if the players position is less than the objects position ( that can be passed through) disable the collider, and when its above the objects position, re-enable that collider.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

2D 360 degress platformer example needed 0 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How to make Rigidbody.AddForce less delayed in Unity3D? 0 Answers

Need help with platformer simple platform script. 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