How to add many OnCollisionEnter in only one Script?
Asked Answered
S

2

0

I want to try a project that only have one Script file (like AS3), so everything is done but when I imagine about collision, I don’t know what must I do. I read everywhere in Google just found OnCollisionEnter but it need to attach the script to one object, that I want is OnCollisionEnter in void Update and can link object like Ball that only get that OnCollisionEnter.
I can add multiple object to be attached in one script with public GameObject anyObject;
Didn’t Understand? Just add a comment :slight_smile:

Salve answered 9/7, 2013 at 9:35 Comment(3)

so i am guessing, void OnCollisionEnter(Collision coll) { foreach (ContactPoint cpoint in coll.contacts) { print(cpoint.otherCollider.name); } }

Varick

can you give me a detail of the script, like difference foreach and if and what will "(ContactPoint cpoint in coll.contacts)" means, and what other thing that must I do to make the collision detector work? I already create the script and attach it to the ball, when I collide with box, nothing printed.

Salve

You can't**.** Accept David's answer and close this.

Noctambulism
G
0

The docs say it’s MonoBehaviour.OnCollisionEnter. This means, it’ll only detect collisions of the current object the script is attached to. It will not detect collisions on other gameObjects.

–David–

Gonnella answered 6/6, 2023 at 1:24 Comment(2)

so, is there any collision detector in void Update?

Salve

No, it doesn't work that way.

Gonnella
O
0

yea if your saying you want 1 script where you tell it what to do if a ball collides and then what to do as well if a box does and also if a player does.

All in one script. Nope.
Also that is simply bad programming from an object oriented point of view.

Each object should deal with it’s own self.

With the way your talking about designing it the ball doesn’t work unless this script that deals with 30 different objects collisions is included.

Thats like me saying i’d like a calculator that does addition and you saying I can’t give you that unless it also does division graphing and provides home phone service.

In as much as possible objects should be self contained and they should be able to be taken out and put in and not break the entire thing. Minimize the amount of dependencies you have.

Ouzel answered 3/7 at 13:35 Comment(1)

Too much script file make me confuse and It was harder to modify variable, that is why I choose to make it in One Script. And also this project is a test project, not real project. I create the test project for gaining Exp and Leveling Up and make everything easier.

Salve

© 2022 - 2024 — McMap. All rights reserved.