- Home /
Function works when called in Start() but wont when called in OnTrigger2D
Im working on making a 2D game (with some 3D elements). My title screen consists of 5 letters, each consisting of Cubes to create each letter. There is an empty parent object (called title). Each letter has an empty game object which contains the parts that compose of the letters. The entire title is Kinematic so it does not fall.
My goal was that when my 2D character hit a 2D trigger, the title would disable its Kinematic state and fall to the ground and bounce a bit on the hidden cylinder near the 2D ground area.
I did some searching on how to access children and wrote a sloppy multi-forLoop function and then swapped it out for a recursive function.
I put an empty game object onto the screen / attached the parent gameobject (the Title) and it worked perfectly. The parts of the letters would fall apart and bounce against each other and hit the floor just like I wanted.
I then created a sprite + box trigger collider, wrote an initial script to test that it would trigger with my 2d character and it did. I then took my recursive kinematic function and added it into the script to activate OnTrigger2D.
At this point it no longer worked. I can get the title break up to work on start() and the 2d objects to trigger with each other in OnTrigger2D, but it wont trigger the function call.
This script was attached to the 2d object with a collider in trigger mode
using UnityEngine;
using System.Collections;
public class KinematicToggle : MonoBehaviour
{
public Transform kinematicDestruction;
void Start ()
{
// Uncommenting this made the title break apart on startup
// RecursiveChild (kinematicDestruction);
}
void Update ()
{
}
void OnTriggerEnter2D(Collider2D other)
{
// This does not activate the function
RecursiveChild (kinematicDestruction);
}
void RecursiveChild(Transform anObject)
{
int children = anObject.transform.childCount; // Checks the number of children
// print ("This object has - " + children);
if (children == 0) // If there are no children
anObject.rigidbody.isKinematic = false; // remove Kinematic state
else
for (int i = 0; i < children; ++i) // For every child call the function again
{
RecursiveChild(anObject.transform.GetChild(i));
}
}
}
Putting debug print statements after the function call would actually force the funtion to call, and through the debug menu it would print out "print ("This object has - " + children)", but it would not change the states of said objects.
I feel that it is probably something simple and because I am new to unity/C# I am really having problems figuring this out.
Any help regarding this would be helpful. Thank you for your time reading this.
Did you put a Debug.Log() statement in the OnTriggerEnter2D() to verify that the callback is getting called?
Here is an alternate to your recursive function:
Rigidbody[] bodies = kinematicDestruction.GetComponentsInChildren<Rigidbody>(gameObject);
foreach (Rigidbody r in bodies) {
r.is$$anonymous$$inematic = false;
}
Answer by RegalPrime · Jul 17, 2014 at 04:19 AM
After doing some extensive debug printing I can confirm that it is changing the states of the objects. I think I remember somewhere among the many many tutorial videos I have watch they were taking about that physics are calculated on startup and that they are not constantly calculated on kinematic object in order to save processing power. Any confirmation of that would be nice to know if it is true.
I added a simple addforce 0 to an object so that it would recalculate forces and it now works like I wanted.
I appreciate the above alternative code. I am not new to programming but I can only use what I know and there are quite a few things I dont know about programming in unity.