- Home /
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
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);
}
}
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.
Your answer
Follow this Question
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