- Home /
how to make a script when an object collides with an object it makes another object rise
I'm trying to make it so when I shoot a target the target is destroyed and I have a blocker raise up to unlock a separate area. I can't get it to make the other object rise, but I have the target disappear. This is what I have so far:
void OnCollisionEnter(Collision theCollision){
if (theCollision.gameObject) {
Destroy (gameObject);
RaiseBlocker();
}
}
void RaiseBlocker(){
curMoveProportion += (Time.deltaTime / moveTime);
if(curMoveProportion >= 1){
if(targetPos == targetPos1){
targetPos = targetPos2;
} else {
targetPos = targetPos1;
}
prevPos = transform.position;
curMoveProportion = 0;
}
transform.position = Vector3.Lerp(prevPos, targetPos, curMoveProportion);
}
I'm confused as to which object this script is attached to. Because it looks you're moving transform.position which means this is the blocker. But then you also have the OnCollisionEnter which means this can be what you shot.
This script is attached to the target. I was trying to make it so when the target was hit it would call the RaiseBlocker method to unlock a 2nd part of the course. I wasn't sure if I had to make a completely different RaiseBlocker script and have this one call the RaiseBlocker script. If so I wasn't sure how to do that.
There's a comment system. Please use it ins$$anonymous$$d of making a new answer. Converted to comment.
It's easier to call another script than to deal with it on this one script that's about to get destroyed after collision.
And please, learn to search:
http://answers.unity3d.com/questions/7555/how-do-i-call-a-function-in-another-gameobjects-sc.html
Answer by GiyomuGames · Aug 11, 2015 at 05:08 AM
agoallenallenallen is right, your code is strange. The script on the target should hold a reference of the object to raise. Then when your target is shot you tell the object-to-raise...to raise.
Answer by B19g_A5s_B411s · Aug 11, 2015 at 03:21 PM
Destroy(gameObject) should be after RaiseBlocker. Functions execute the script in order.
Your answer
![](https://koobas.hobune.stream/wayback/20220611235247im_/https://answers.unity.com/themes/thub/images/avi.jpg)