Tagged gameObjects and OnCollisionEnter function

Hey there, I’m rather green with Unity and javascript. Got a few questions:

First of all… I know a gameObject can only have one tag, however can multiple gameObjects in a scene share the same tag?

I’m trying to create an OnCollisionEnter function that recognizes precisely what gameObject it has collided with and play an AudioClip accordingly. I know collisionInfo includes the gameObject information, I just can’t figure out how to discern between different objects within the function:

var trigger_magnitude = 2;
var desk_hit : AudioClip;
var wall_hit : AudioClip;
var chair_hit : AudioClip;
var plant_hit : AudioClip;
var proj_hit : AudioClip;

function OnCollisionEnter(collision : Collision) {

	
	if(gameObject == gameObject.FindWithTag ("Desk")){
		
		AudioSource.PlayClipAtPoint(desk_hit, transform.position);
		
		if(collision.relativeVelocity.magnitude > trigger_magnitude){
			audio.volume = 1;
		}else{
			audio.volume = .5;
		}
	
	}if(gameObject == gameObject.FindWithTag ("Wall")){
		
		AudioSource.PlayClipAtPoint(wall_hit, transform.position);
		
		if(collision.relativeVelocity.magnitude > trigger_magnitude){
			audio.volume = 1;
		}else{
			audio.volume = .5;
		}
		
	}if(gameObject == gameObject.FindWithTag ("Plant")){
		
		AudioSource.PlayClipAtPoint(plant_hit, transform.position);
		
		if(collision.relativeVelocity.magnitude > trigger_magnitude){
			audio.volume = 1;
		}else{
			audio.volume = .5;
		}


	}if(gameObject == gameObject.FindWithTag ("Chair")){
		
		AudioSource.PlayClipAtPoint(chair_hit, transform.position);
		
		if(collision.relativeVelocity.magnitude > trigger_magnitude){
			audio.volume = 1;
		}else{
			audio.volume = .5;
		}

	}if(gameObject == gameObject.FindWithTag ("Projectile")){
		
		AudioSource.PlayClipAtPoint(proj_hit, transform.position);
		
		if(collision.relativeVelocity.magnitude > trigger_magnitude){
			audio.volume = 1;
		}else{
			audio.volume = .5;
		}
	}
}

Any help would be much appreciated!

Thanks in advance

-Ace

Yes, multiple game objects can share a tag. However you seem to have things bass ackwards.

In your code, gameObject is referring to the the object the script is attached to. Not the one that has collided with it. You want to pull the game object from the collision info, such as.

function OnCollisionEnter(collisionInfo : Collision) { 

 if (collisionInfo.gameObject.tag == "Tag1")
  DoSomething()

}

I’m also not sure if you need PlayClipAtPoint, likely PlayOnce would be good enough if the audio source is attached to the game object, which is located at the collision. Just make sure it’s imported as Mono if you want directional sound.

I did indeed have everything bass ackwards. Thanks!

-Ace