- Home /
The question is answered, right answer was accepted
Trying to trigger something on collision.
I'm making a basic thing for class. What i am trying to do is this. When i throw a ball and it connects with the collision of the box i have, i want it to disappear and have my shattered box prefab come up. The problem i am having is the colision. I can't get this script to work. Can anyone help? Using javascript.
var remains: GameObject;
function OnCollisionEnter (collision : collision)
if (Collision.gameObject == something);
function Update()
{
{
Instantiate(remains, transform.position, transform.rotation);
Destroy(gameObject);
}
}
Answer by Jeff-Kesselman · Nov 17, 2014 at 04:11 PM
Your script makes no sense.
Two problems with this line:
if (Collision.gameObject == something);
First, I don't see "something" defined anywhere. Secondly, it has no body just a ; at the end so it does nothing even if true.
This line also appears to do nothing:
function OnCollisionEnter (collision : collision)
You really need to get yourself a book on basic Javascript or read through a coding tutorial. This looks like cut and paste by someone who had no idea what they were cutting and pasting.
So would something like this make more sense?
var remains: GameObject;
function OnCollisionEnter (col : Collision)
{
if(col.gameObject.name == "throwable_ball")
{
Instantiate(remains, transform.position, transform.rotation);
Destroy(gameObject);
}
}
$$anonymous$$UCH more sense!
Now, what does that code do or not do, compared to what you intend it to do?
What i intend it to do is once the "Throwable_ball" hits the collision box of a cube, it is supposed to destroy the box and spawn cube "Shards" (Like a glass box shattered) where the cube was. What it does, is not that. When the ball hits the cube, it is supposed to destroy the cube, but it does not.
Okay, the very first thing you should do is add a debug inside the if block to see if its getting there at all.
if not then there is something wrong with the name you are checking. Note that if the ball was made from a prefab it WONT have the same name as the prefab.
A cleaner way to do this, rather then with object name, is to create a "thrown_ball' tag and check that ins$$anonymous$$d. if the prefab has a tag, its spawned instances will also have the same tag.
Okay, the very first thing you should do is add a Debug.Log() inside the if block to see if its getting there at all.
if not then there is something wrong with the name you are checking. Note that if the ball was made from a prefab it WONT have the same name as the prefab.
A cleaner way to do this, rather then with object name, is to create a "thrown_ball' tag and check that ins$$anonymous$$d. if the prefab has a tag, its spawned instances will also have the same tag.
Follow this Question
Related Questions
Assign Default For Large Array 1 Answer
How i make this code in Unity Javascript 2 Answers
Can someone help me fix my Javascript for Flickering Light? 6 Answers
Moving on Axis 1 Answer
Difference between GetComponent and transform.GetComponent? 1 Answer