How to detect collision between two objects using a script attached to the main camera

Pls help! I have a code that detects collison when my game character hits a cube and when this collision happens, its makes a sound, this script is attached to the game character. pls is there a way I can get the same result if I attach the script to the camera. I want to get the same impact noise when my game character touches the cube but with the script attached to the main camera. This is the code I used
The game character is tagged as player

Function OnControllerColliderHit (hit : ControllerColliderHit)
{
If (hit.gameObject.tag == "Enemy") //the cube is tagged as enemy
{
Audio.PlayOneShot (impact);
}
}   

Pls any help would be greatly appreciated… thanks in advance

In your Camera Script add two public GameObject/transform caches at the top
and add your two cubes in to the script at the inspector panel by dragging them in or using the UI to navigate to them.

Once they are assigned, you can use them and anything attached to them as a component by calling it through their scope (or Dot “.”) operator.
Hence, you could find the value/hit count you wanted by a simple if statement in your cube script and pass that over to a new variable, for example “colliderCount” in your Camera script.

You could assign it as such…

void Start()
{
//collisionhits could be the variable you assign to be... 
//incremented in your gameobjects that are colliding etc.
colliderCount = myTransformCache.collisionhits;
}

of course you would have to update this colliderCount cache during the game update so, repeat the statement found in Start() inside your Update() like so…

void Update()
{
 
//place anywhere after your collision logic 
colliderCount = myTransformCache.collisionhits;
//place anywhere before you need to change it again
}

FOR your GUI to get the above you might try something like…

void OnGUI()
{
GUI.Box(new Rect(10, 10, 150, 100), colliderCount); //colliderCount is what im using as example var name remember
}

My main grief with all this is that you are going around the houses a little, as metioned by the other guys here.

to simplify all the above, for now to get you going…
make a static cache for your score …call it… hitScore

public static int hitScore;

place that in your cube`s collision script.

at the conditional to check for a collision,where it will evidently be checking to see if a collision has happened or not…in the “has happened” (true) part, place the below bit.

hitScore ++;

Now in your Camera Script, you can simply work with it.
Best route for you from here?
cache the script hitScore resides in and you can work with it as an instance.
so…

public MyCollisionScript script;

…drag your script into the inspector panel of the script attached to your camera.

NOW for your GUI problem (this should really be another question but suppose it`s related succinctly n all that so…)

place a method like this below, into your camera script

void OnGUI()
{
GUI.Box(new Rect(10, 10, 150, 100), myCollisionScript.hitScore.ToString()); //this //will address the instance holding the hitScore value in the collision script and will //send it to the GUI display box as a string representative for you.
}

CAVEAT: there are better ways to do this and, of course, we are taking advantage of static which you should soon discover is not a great practice as things become easy to abuse and RunTime is riddled with passing static objects around the place that could of been encapsulated(hidden) instead. however, for now, to solve your immediate problem.

Let me know if it doesnt help, cos i can see ive written allot here and can imagine its a fair whack to take in at any one time, leading to confusion maybe.
happy to help further. And… one last thing.

Do you want to understand code or develop games?
I humbly ask this because i think you may benefit from reading a few tutorials online, namely the Unity ones are a good start and oddly go through this pickle with you in their beginner tutorials… here`s the link dude.
[Unity Tutorials Page][1]

Take care and hope you get it sorted…and if this did help, marking it as an answer will only serve to help others looking at the same kinds of problem
gruffy :slight_smile:
[1]: Learn game development w/ Unity | Courses & tutorials in game design, VR, AR, & Real-time 3D | Unity Learn