- Home /
Call Builtin Function on other GameObject
Hello, allow me to illustrate what I want to do:
Let's say we have 1 script named script, and 2 gameObjects: object A and object B. The script is attached to object A. The following code is inside script:
function OnCollisionEnter() {
//when something collides with object A
print("hi");
}
But what I want to do is find out when something collides with object B, not Object A.
Is there any way to have the "OnCollisionEnter()" function apply to object B without attaching the script to Object B?
EDIT: I want to do this so that I don't have to have multiple instances of code running at one time. For example, I would only need to have one script that controls when something collided with all the bullets (object B) in the scene, even when there are multiple bullets.
Ohh, I get you now, I do not believe it is possible for you to do it without actually attaching the script to object B, the only way I can think of is setting object B as object A's child. Tell me what you find out!
Answer by happyalex198 · Aug 15, 2011 at 11:57 PM
OnCollisionEnter has a Collision parameter that you can use to access the object that you have collided with. For example, to get the name of the game object that object A collided with you could do:
void OnCollisionEnter(Collision collision) {
print(collision.gameObject.name);
}
I want to detect when something collides with Object B, not if object B is the "collision". But I suppose I could put this in the objects that I am checking collision against. Thanks!
Your answer
Follow this Question
Related Questions
Wy does this destroy script not work? 2 Answers
Restart this script 3 Answers
Stop object spawning on top of each other? 1 Answer
Trying to make a simple inventory: 0 Answers