- Home /
Detect Collision with Instantiated Object Child
I have a prefab with a child that has a kinematic Rigidbody 2D and a Box Collider 2D that is a Trigger. The prefab gets instantiated using this script:
static var canSpawn = true;
var platform : GameObject;
var heightMax : Transform;
var heightMin : Transform;
var time : float;
function Update () {
var mousePos = Input.mousePosition;
mousePos.z = 10;
var objectPos = Camera.main.ScreenToWorldPoint(mousePos);
if(Input.GetButtonDown("Fire1") && objectPos.y < heightMax.position.y - 0.5 && objectPos.y > heightMin.position.y + 0.5 && canSpawn){
var clone = Instantiate(platform, objectPos, Quaternion.identity);
Destroy(clone, time);
}
}
with platform being the prefab.
Attached to the player - which also has a kinematic Rigidbody 2D and a Box Collider 2D - is this script:
var left : Transform;
var up : float;
var back : float;
var time : float;
function OnTriggerEnter2D (other : Collider2D) {
if(other.transform == left){
Debug.Log("test");
ConstantMovement.canMove = false;
rigidbody2D.velocity.y = up;
rigidbody2D.velocity.x -= back;
}
}
function OnTriggerExit2D (other : Collider2D) {
if(other.transform == left){
yield WaitForSeconds(time);
ConstantMovement.canMove = true;
}
}
This works as expected when placing the prefab in the scene and attaching the child to the Transform left. However, when using it with the instantiated objects, it fails at "if(other.transform == left)". I believe this is because the instantiated objects are being created as clones. So how should I go about detecting the collision with the clones?
Answer by Kiwasi · Jun 11, 2014 at 07:26 PM
Other.transfom == left will only return true if the transforms are the same GameObject
To check if they are made from the same prefab you can use tags or check the name.
So I tried this.
function OnCollisionEnter2D (collision : Collision2D) {
Debug.Log("Test1");
if(collision.gameObject == GameObject.FindGameObjectWithTag("Left")){
Debug.Log("Test2");
}
}
I added the tag "Left" to the child of the prefab that's being instantiated. Well, needless to say it's not working. Without Googling and fooling around with it for another 4 hours, that's the best I've got. I'm quite new to all of this, so I'm sorry if I'm missing something obvious. Any advice would be appreciated.
Another, somewhat related question, wouldn't "collision.gameObject == GameObject.FindGameObjectWithTag("Left")" be an example of "hard coding" which should be avoided? Perhaps someone could clear that up for me. If it's not, what exactly is "hard coding"?
Edit: The Is Trigger has been removed from the child, of course. It passes up to Debug.Log("Test1").
After taking a break and doing a bit of Googling, I found that the correct way (well, working in this situation anyway) to be collision.gameObject.tag. So the script is now this:
function OnCollisionEnter2D (collision : Collision2D) {
if(collision.gameObject.tag == "Left"){
Constant$$anonymous$$ovement.can$$anonymous$$ove = false;
rigidbody2D.velocity.y = up;
rigidbody2D.velocity.x -= back;
}
}
function OnCollisionExit2D (collision : Collision2D) {
if(collision.gameObject.tag == "Left"){
yield WaitForSeconds(time);
Constant$$anonymous$$ovement.can$$anonymous$$ove = true;
}
}
However, isn't this still "hard coded"?
Glad you got it figured. The second code is what I meant.
Rules against hard coding are slightly more flexible in game development. Efficiency is typically more important then reusability. DRY principles are still well worth following.
If you are concerned about hard coding then tag is a string, so you can fix it like this. (C#, but easy enough to convert using the docs)
// This could be public as well if you wanted to assign it via the inspector
private string left ="Left";
void OnCollisionEnter2D (Collision2D collision){
if(collision.gameObject.Tag == left){
//Do all that other stuff
}
}
Your answer
Follow this Question
Related Questions
Getting instance of an sub object rather than the original's subobject 0 Answers
Renaming Child Objects during Instantiate? 1 Answer
Instantiate Terrain Object as child of Empty Game Object 1 Answer
Instantiate GameObject Parent targetting issue. 0 Answers
instantiated Gameobject trying to access child of child which is a clone itself. 2 Answers