- Home /
Collision code not working?
I have a project in which you collide smaller object to create bigger ones. When two small objects (Brick and Stick) collide, they destroy themselves and instantiate the larger object (Building1) in the player's position. Here is my code so far:
#pragma strict
var currentPosition = GameObject.Find("Player");
var Building1: GameObject;
function OnCollisionEnter(Col : Collision)
{
if(Col.collider.gameObject.name == "Stick")
{
Destroy(gameObject);
Destroy (Col.collider.gameObject);
var create = Instantiate(Building1, currentPosition.transform.position, Quaternion.identity);
}
}
For some reason, nothing happens when the two collide. I have made sure the tags and names of gameobjects are correct. What has gone wrong and how can I fix it?
Thanks in advance.
Answer by imp903 · Jun 19, 2015 at 03:12 AM
Just as a side note, i would use tag, not name, so that if any sticks are created by script, any name differences will be accounted for and you won't have to worry about it. The problem might be in your reference to currentPosition. Referencing GameObject.Find outside of a function can create a lot of problems. I would just use a variable instead (var currentPosition : GameObject;) or put GameObject.Find inside of the start function. Also i'm fairy certain gameObject is not needed between collider and name in your if statement. Lastly, just make sure at least one of the two objects has a rigidbody component or nothing will happen at all, which is also a very likely culprit. I can't say for sure because there's no error message here, but these are what I would try first if I were you.
Your answer
Follow this Question
Related Questions
FPS spawn bullet 0 Answers
Var CollisionObject not working. 1 Answer
Enemy death help 1 Answer
Network.Destroy and collision; destroying correct object. 1 Answer
Gun Fire, sparks on Collision 1 Answer