Collition help

I’ve scripted a object so when one button is pressed it moves across the screen in one direction and when another button is pressed it does the opposite. However the Script is on the Button and not on the object so I can’t stop it from going thought the walls using the “OnCollisionEnter” function in the script. Is there anyway to control the object when it hits a wall without “OnCollisionEnter”?

There are two solutions I can think of here:

First, you could create a separate script that you add to the object itself. Then, you can have an OnCollisionEnter on the object and, when it hits a wall, force it to change direction. That way, your button would only care about changing direction, and the object itself can worry about dealing with walls.

The second option is to use the FixedUpdate on the button script to perform a raycast from the object location in the direction it is moving. You could then possibly use the raycast results to determine if the object has a wall right in front of him. If he does, you could then make him turn around. See Unity - Scripting API: Physics.Raycast

Personally, I prefer the first option because it splits clear responsibilities between scripts, and it also handles hitting the wall in an event-based way rather than polling every frame.