Can I access children's colliders from the parent?

Hi guys I’m trying to do a 2d-game where it uses tetris shapes. I’m having problems with the collision. And this is what i did.

  1. Create a cube. Set size to be 64x64. Check (tick/enable) the “IsTrigger” in box collision.

  2. Create a empty game object and drag the cube into this empty game object

  3. Copy the cube and creates 3 other instances of that cube. Arrange it to make an inverted “L” shape.



← gameobjects position.

Now i want to make it such that the box collider follows this inverted “L” shape. How do i do that? Oh and here is my script which i attached to my gameobject

// Update is called once per frame
	void Update () {
		// if falling is allowed
		if (m_bIsFalling)
		{
			Fall();
		}
		
	}
	
	void Fall()
	{
		this.transform.position = new Vector3( this.transform.position.x, this.transform.position.y - Falling_Speed, this.transform.position.z );
	}
	
	void OnTriggerEnter(Collider other)
	{
		if (other.gameObject.tag == "Ground" || other.gameObject.tag == "Wall")
		{
			this.transform.position = new Vector3(other.gameObject.transform.position.x ,this.transform.position.y, this.transform.position.z);
			m_bIsFalling = false;
		}
	}

which was initially attached to each individual cube with this code instead:

// Update is called once per frame
	void Update () {
		// if falling is allowed
		if (m_bIsFalling)
		{
			Fall();
		}
	}
	
	void Fall()
	{
		this.transform.Translate(new Vector3( 0, - Falling_Speed, 0 ) );
	}
	
	void OnTriggerEnter(Collider other)
	{
		if (other.gameObject.tag == "Ground" || other.gameObject.tag == "Wall" || other.gameObject.tag == "TPBlock")
		{
			this.transform.position.Set(other.gameObject.transform.position.x ,this.transform.position.y, this.transform.position.z);
			m_bIsFalling = false;
		}
	}

I also tried using fixed hinges to make the 4 cubes act as 1 object but that failed miserably. Can anyone help me here? Any help is appreciated. Thanks in advance.

The structure of this sounds confusing, but to answer the question in the title, yes you CAN access children of a gameobject. In a class in the parent you can access children using: foreach(Transform child in transform) { Collider childCollider = child.GetComponent<Collider>(); // do stuff with the collider }

so how do i call the OnTriggerEnter function inside there? for eg. in the OnTriggerEnter function for each individual child i want to put void OnTriggerEnter(Collider other) { if (other.gameObject.tag == "Ground" || other.gameObject.tag == "Wall" || other.gameObject.tag == "TPBlock") { //this.transform.position = new Vector3(other.gameObject.transform.position.x ,this.transform.position.y, this.transform.position.z); m_bIsFalling = false; } } where it checks the tag of the collider and see if its "Ground" or whatever.

and thanks for helping me out. much appreciated

Thanks alot Liamcary! You've been such a great help! Many thanks!

1 Answer

1

This works thanks!

using UnityEngine;
using System.Collections;

public class TetrisPeopleBlock : MonoBehaviour {
	// the speed of which the tetrispeople block falls
	public float Falling_Speed;
	[HideInInspector] public bool m_bIsFalling;

	// Use this for initialization
	void Start () {
		m_bIsFalling = true;
		Falling_Speed = 2;
	}
	
	// Update is called once per frame
	void Update () {
		if (m_bIsFalling)
	    {
	    	Fall();
	    }
	}
	
	void Fall()
    {
    	this.transform.position = new Vector3( this.transform.position.x, this.transform.position.y - Falling_Speed, this.transform.position.z );
    }	
	
	void OnTriggerEnter(Collider other)
	{
		print("hit");
		m_bIsFalling = false;
		
		foreach(Transform sibling in transform.parent)
		{
			TetrisPeopleBlock script = GetComponent<TetrisPeopleBlock>();
			
			if(script != null)
			{
				script.m_bIsFalling = false;
			}
		}
	}
}

You're very welcome! Though i just noticed an error in my last comment. In the foreach statement in the OnTriggerEnter function it should be "sibling.GetComponent()". Otherwise you're just accessing the component of the block that had the collision multiple times, rather than accessing the component of all of the other blocks. (Line 35 in your last comment)

wow thank you! it works even better. because before it had a kind of "bouncing" effect but once i changed to foreach(Transform sibling in transform.parent) { TetrisPeopleBlock script = sibling. GetComponent<TetrisPeopleBlock>(); if(script != null) { script.m_bIsFalling = false; } } the undesirable "bounce" effect was removed. Many thanks! :D