Why are collisions not working?

Sure. Some might say this question has been asked too many times to bother with (I read “ad nauseum” in one post), but the documentation and all google searches yield no answers.

Anyways, I used the template from the documentation in order to create a rigidbody projectile…

// Instantiate a rigidbody then set the velocity

var projectile : Rigidbody;

function Update () {
// Ctrl was pressed, launch a projectile
if (Input.GetButtonDown("Fire1")) {
// Instantiate the projectile at the position and rotation of this transform
var clone : Rigidbody;
clone = Instantiate(projectile, transform.position, transform.rotation);

// Give the cloned object an initial velocity along the current
// object's Z axis
clone.velocity = transform.TransformDirection (Vector3.forward * 10);
}
}

And this one for the collision which is attached to my terrain…

function OnCollisionEnter (other : Collider) {
	
	if (other.name == "clone") {
		Destroy (other.gameObject);
		}
	}

From what I have gleaned from both the documentation, google searches, and unity answers searches, this would seem the way to do it. But apparently it’s not. My gut tells me it has to something to do with the fact that I am make a new rigidbody from a prefab (clone), and that because of that, I need to do something else to access each individual “clone”. I don’t know as I have been starting at this issue for too long for it to make any sense… As of now, my projectiles bounce off of my terrain.

3 Answers

3

You’re comparing apples and oranges. clone is the variable’s name, not the name of the object you’ve instantiated (its name is the same of the prefab, perhaps with a “(clone)” string appended - Unity has misterious reasons to append it or not).

A simple way to work around this is to name explicitly the object right after its creation:

  var clone : Rigidbody;
  clone = Instantiate(projectile, transform.position, transform.rotation);
  clone.name = "clone";

The last line defines the object name to “clone”, thus your comparison can work.

In Unit, the object name is a String variable (property, to be more specific) which can be accessed with collider.name or transform.name (read inherited variables in Collider or Transform in the docs)

When you instantiate a prefab, it always has "(Clone)" appended, unless you rename it yourself.

Aldonalettos work around will work for you but it may be another idea to give your projectile prefab a “projectile” tag and compare the collided objects tag instead.

I'm using the answer section instead of the comment section so I can format it for legibility.

@ aldonaletto - What you say makes perfect sense, but my projectiles still bounce off of my terrain. Here is the actual code as it exists right now...

var muzzleFlash : Transform;
var turret : Transform;
var mainGun : Transform;
var recoil : boolean = false;
var reload : int = 0;
var fireSound : AudioClip;
var projectile : Rigidbody;

function Update() {
//are we shooting?
var firing : float = Input.GetAxis("Fire");

if (firing != 0) {

//if the main gun is not in a state of recoil, then fire!
    if (recoil == false) {

//fire effects and sound
        Instantiate (muzzleFlash, transform.position, transform.rotation);
        recoil = true;
        audio.PlayOneShot(fireSound);
        mainGun.Translate (Vector3(0,0,-.07));  

//make projectile
        var clone : Rigidbody;

        clone = Instantiate(projectile, transform.position, transform.rotation);
        clone.velocity = transform.TransformDirection (Vector3.forward * 50);
        clone.name = "clone";
        }
    }

//if in state of recoil, recover
if (recoil == true) {

    mainGun.Translate (Vector3(0,0,.0014));
    reload += 1;
    if (reload == 50) {
        reload = 0;
        recoil = false;
        }
    }
}

Second script is still...

function OnCollisionEnter (other : Collision) {

    if (other.name == "clone") {
        Destroy (other.gameObject);
        }
    }

Also, I just noticed this error...

Script error: OnCollisionEnter This message parameter has to be of type: Collision The message will be ignored.

What does it mean? Google and answers search keep mentioning "Make sure you use Collision instead of Collider".

Please don't post comments as answers--you can format code the same in comments. (Although it's easier in answers, so you can write as an answer, then copy and paste it to a comment.)

I missed this error: OnCollisionEnter receives a Collision object, not a Collider. @Blankzz is right: Collision.name doesn't exist, but Collision.gameObject.name is ok.