- Home /
More than 1 object in oncollisionenter
Hello guys!!! if some 1 know can i put in OncollisionEnter more that 1 object this is script:
function OnCollisionEnter(Gun : Collision){
if (Gun.gameObject.tag == "gun") {
isparent=true;
isPlayingSound=true;
destroyaudiosource.Play();
GunpositionTransform.parent=transform;
GunpositionTransform.localPosition = Vector3(-3.122956,-0.01368039,0.04606679);
Update();
}
else {
if (Gun.gameObject.tag == "mesh"){
} } }
and i wonna parent 2 more other object whith other position to parent if collision is detected. how can i do that if :
function OnCollisionEnter(gun:Collison || gun2:Collision)----->not work
i just won to know how can i add more then 1 object to OnCollisionEnter. i think for that its not needed the code but i will show he's big:
pragma strict
var destroy :AudioClip;
private var destroyaudiosource:AudioSource;
private var isPlayingSound:boolean;
var bullet: Transform;
var bulletspeed: float=5000;
var isparent:boolean;
private var placespawn: GameObject;
var GunpositionTransform=placespawn.transform;
function Start(){
destroyaudiosource=gameObject.AddComponent(AudioSource);
destroyaudiosource.clip=destroy;
destroyaudiosource.loop=false;
destroyaudiosource.volume=1.0;
}
function Update(){
if (Input.GetButtonDown("Fire1") && isparent==true){
var bulletCreate = Instantiate (bullet,GameObject.Find("spawnposition1").transform.position,Quaternion.identity);
bulletCreate.rigidbody.AddForce(transform.forward * bulletspeed);
}
else {
if (Input.GetButtonDown("Fire1") && isparent==false){
Debug.Log("You Dont have any waypon! and that is your problem.");
}
}
}
function OnCollisionEnter(Gun3 : Collision){
if (Gun3.gameObject.tag == "gun2") {
isparent=true;
isPlayingSound=true;
destroyaudiosource.Play();
GunpositionTransform.parent=transform;
GunpositionTransform.localPosition = Vector3(-0.2782021,0.3591104,-5.368056);
Update();
}
else {
if (Gun3.gameObject.tag == "mesh"){
} else { isparent=false;
}
} }
I think you got me wrong, I did not mean you need to fully post it, I meant you need to select code and press 101010 or ctrl+k to make it look like code. Did you check the answer anyway?
i'm not checked but i reading and i think this is not what i need, but thx you verry much anyway. I just put 3 script with OnCollisionEnter in 1 object :) its work fine
Answer by fafase · Apr 20, 2012 at 04:09 PM
function OnCollisionEnter(gun:Collision){
if(other.gameObject.tag=="gun"||other.gameObject.tag=="mesh"){
// Do the same to both
}
}
function OnCollisionEnter(gun:Collision){
if(other.gameObject.tag=="gun"){
// Do something with gun
}
else if(other.gameObject.tag=="mesh"){
// Do smg to mesh
}
}
Is that what you meant? Try to post properly, your code shows all messed up, it is hard to figure out what you already have and what you do wrong.
Resolve problem: I just put 3 script (because i need to detect 3 object) with OnCollisionEnter in 1 object :) and its work fine
Your answer
![](https://koobas.hobune.stream/wayback/20220613070546im_/https://answers.unity.com/themes/thub/images/avi.jpg)