Search Unity

Question Detect collisions of objects in another script

Discussion in 'Scripting' started by Effsty, Apr 21, 2023.

  1. Effsty

    Effsty

    Joined:
    Jun 24, 2016
    Posts:
    29


    I have 3 objects in the scene, player on the left, a red ball and a green box.

    I want to detect when the red ball collides with the green box but - for whatever out of this world reason - I want to check it in player's script.

    So while I could attach a script to the ball and add a OnCollissionEnter() function I would like to write a function on the parents script to check any ball collisions.

    How can I achieve this?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,752
    You're gonna have a LOT of mysterious problems if you keep misspelling callback methods!

    Misspelled methods will NOT be called, so it's on you to get it correct. Intellisense should help you.

    This may help you with intellisense and possibly other Visual Studio integration problems:

    Sometimes the fix is as simple as doing Assets -> Open C# Project from Unity. Other times it requires more.

    Other times it requires you also nuke the userprefs and .vsconfig and other crufty low-value high-hassle files that Visual Studio tends to slowly damage over time, then try the above trick.

    Barring all that, move on to other ideas:

    https://forum.unity.com/threads/intellisense-not-working-with-visual-studio-fix.836599/

    Also, try update the VSCode package inside of Unity: Window -> Package Manager -> Search for Visual Studio Code Editor -> Press the Update button

    Also, this: https://forum.unity.com/threads/no-suggestions-in-vscode.955197/#post-6227874

    Referencing variables, fields, methods (anything non-static) in other script instances:

    https://forum.unity.com/threads/hel...-vars-in-another-script.1076825/#post-6944639

    https://forum.unity.com/threads/accessing-a-gameobject-in-different-scene.1103239/

    REMEMBER: it isn't always the best idea for everything to access everything else all over the place. For instance, it is BAD for the player to reach into an enemy and reduce his health.

    Instead there should be a function you call on the enemy to reduce his health. All the same rules apply for the above steps: the function must be public AND you need a reference to the class instance.

    That way the enemy (and only the enemy) has code to reduce his health and simultaneously do anything else, such as kill him or make him reel from the impact, and all that code is centralized in one place.
     
  3. seejayjames

    seejayjames

    Joined:
    Jan 28, 2013
    Posts:
    691
    Highly recommend any checks about an object (like collision) be done on the object, and any info about it be sent "upwards" if needed for overall tracking of states. "Downward" commands should be running methods that are on the object, not changing values directly, as mentioned above (if the event should cause other events to happen, that's all centralized on the object itself). It might be tempting to have the player object manage everything, but I've found it to get convoluted quickly.

    That said, communication between scripts on any GameObjects is possible, so you can construct it however you wish...