This seem so simple (sorry), but…
I have created a ball and two color boxes; one green and one red. I want to write a script that will create an explosion when it hits the red and pass nothing when it hits the green. I assume I need to create a scrip in the red box that calls OnCollisionEnter, but can not figure out how to create the IF statement to determine if theBall has run into the red box.
Thanks!
var theBall : GameObject;
var explosion : GameObject;
function OnCollisionEnter () {
If ????? = theBall {
if (explosion != null) {
instantiatedExplosion = Instantiate (explosion, transform.position, transform.rotation);
Destroy (instantiatedExplosion, 10);
}
Destroy (gameObject);
}
}
Xacto,
“OnCollisionEnter ()” can take an argument, whose value is the object colliding with it ie. try something like this
var myBall : GameObject;
function OnCollisionEnter ( thingHittingMe){
if(thingHittingMe == myBall){
blah blah..
}
You can then attach “myBall” to the ball via the inspector.
Richard.
To be a little more precise, here is an example:
function OnCollisionEnter(colInfo){
if( colInfo.rigidbody.name == "ballName") {
renderer.material.color = Color.red;
}
}
:?
Hmm… I am getting the following error:
system.NullReferenceException
UnityEngine.Object:get_name()
var strikeObject : GameObject;
function OnCollisionEnter(colInfo){
if( colInfo.rigidbody.name == "strikeObject") {
renderer.material.color = Color.red;
}
}
You probably want to use:
if( colInfo.other.name == “strikeObject”) {
It gave you a null exception because the other object happened to be a collider with no rigid body thus colInfo.rigidbody is null.
Take a look at the script reference of Collision for more information.