Trying to trigger a message when box A collides with box B. Have no idea what to do..

Hi there,

I have a cube that the player can pick up and carry around (Cube A) and a static holder (Cube B) which the cube can be dropped into. If I wanted to trigger a simple message when Cube A is dropped on Cube B on screen how could I do this?

Thanks for the help,

Max

Hi mate,

If you tag Cube A then you can use it in this function to determine if it has touched Cube B.
Make sure Cube A is set to trigger and place a script like this on Cube B.

void OnTriggerEnter(Collider col)
{
    if(col.tag == "Cube A")
    {
        debug.log("Cube A Entered");
    }
}

This will print a message in the console.
Once you have this working then you can use GUI to print a message to the screen if that’s what you wanted.

Hope this is what you mean, its worth having a look on Youtube for some trigger and GUI tutorials

Thanks mate, that’s pretty much what I’m looking for to get me started although when I attach the script I get a bunch of errors.

A couple more similar ones.

The sample code i put there is c#, sorry i should of said, I’m not sure how the trigger functions differ from javascript. Post the whole script for the Cube on and ill have a look. It looks like you are just getting some syntax errors.

The code I’m using is -

“Triggerer” is the tag on the cube

Getting the following error:

if(col.tag == (“Triggerer”))

Sorry missed a couple brackets! wrote it in a bit of a rush
Try that !

Still getting the same error I’m afraid.

The first line is underlined in red in MonoDevelop if that means anything?

Have you got the script set up right with everything in the main class brackets?

using UnityEngine;
using System.Collections;
 
public class THESCRIPTSNAME : MonoBehaviour {



void OnTriggerEnter (Collider col)
{

.............
}

}

It wasn’t but I’m still getting the same error -

Full script:

Added another } to the end which removes the error but then gives this error

Capital D!
Debug.log
nearly there lol

Haha we have it! It works!

Kind of…

Haha, thanks James. If you’re willing to help some more then any ideas why the cube now falls through cube b? It doesnt pay attention to the collision like it did previously?

Thats cool, Yeah its because Cube A is set to trigger, when an object is a trigger it won’t collide. Try putting a Rigidbody component on it.

The error from the start was a missing semi-colin. His original script + Fixed:

void OnTriggerEnter(Collider col)

{

    if(col.tag == "Cube A");

    {

        Debug.Log("Cube A Entered");

    }

}

This is in C# (C Sharp)

Also you were missing some caps.

Or if you deleted your entire script (The void, and using stuff) Then just copy paste this code:
(Still C#)

using UnityEngine;
using System.Collections;

public class Collide : MonoBehaviour { /* Rename "Collide" to your current script name */

	void OnTriggerEnter(Collider col)
		
	{
		
		if(col.tag == "Cube A") /* The tag the cube needs */
			
		{
			
			Debug.Log("Touched Cube A"); /* Tell the console they touched */
			
		}
		
	}

}

This code represents that the cube needs to have a tag called “Cube A”
And that the script itself needs to be named “Collide” I have shown where this can be changed using a comm