Help with OnTriggerEntered + Canvas UI [closed]

This seems like it should be incredibly easy and I know there are other posts out there with a similar title, but I promise you I have looked at all of them. All I want is for my canvas that simply says "you win" to appear when I enter an invisible box collider that I have . My canvas is by default set to disabled.

public class YouWin : MonoBehaviour {
public Canvas myCanvas;

private void Start()
{
myCanvas = GetComponent<Canvas>();

}

void OnTriggerEnter (Collision Collider)
{
{
myCanvas.gameObject.SetActive(true);
}
}


This script seems incredibly simple and straightforward, but for whatever reason, when I enter my box, nothing at all happens. Why might this be? What am I missing?

As per an answer I also tried

public class YouWin : MonoBehaviour {
public GameObject canvas;

void OnTriggerEnter (Collider Collider)
{
{
canvas.gameObject.SetActive(true);
}
}


But had no luck with this either. I've been tinkering to no avail, can you please give me some suggestions?

closed as off-topic by DMGregory♦Aug 1 '18 at 2:46

This question appears to be off-topic. The users who voted to close gave this specific reason:

• "Questions about debugging a problem must provide a minimal, complete, verifiable example of the issue so that readers can diagnose it without needing to guess, read all of your code, or engage in extensive back-and-forth dialog. For more information, see this meta thread." – DMGregory
If this question can be reworded to fit the rules in the help center, please edit the question.

• Is this script on the object with the canvas? – Superhq 2000 Jul 30 '17 at 20:40
• the script is attached to the box that i collide with – BondyeLwa Jul 30 '17 at 20:52
• Why are you adding redundant brackets though? – Sidar Jul 31 '17 at 12:55
• It is just leftover from trying other stuff. That isn't what's causing the problem though. – BondyeLwa Jul 31 '17 at 16:38
• Have you checked the collider table to see whether your collider systems are compatible? It could be the case that you're just combine the wrong collider types. – sjoerd216 Mar 6 '18 at 7:25

It's unclear what exactly is wrong, here are a few things can could be wrong:

• Since you are using OnTriggerEnter() make sure the object attached to this script, has a Collider set with isTrigger to true.
• I'm not sure why you would attach a Canvas to an object that seems to resemble an actual object (either 2D or 3D). Try making the object with the Canvas a separate object, linked with public Canvas canvas; (also suggested by another answer). The idea of the Canvas is that it draws its children, so there (usually) isn't a need to make an object both draw stuff on the screen and being a Canvas.
• Maybe OnTriggerEnter() activates as expected, but you just don't see anything because you only enable a Canvas element in it, which could theoretically draw nothing on top of the screen (depending how you set it up). Try replacing myCanvas.gameObject.setActive(true); with something like Debug.Log("canvas enables itself now");, when running the game, take a look in the console to see if this runs or not.
• Also make sure that at least one of your objects has a RigidBody attached (Thanks @DMGregory), otherwise collisions are not triggered. You can take a look here for a table explaining how Unity calculates collisions and triggers.

It might also be useful to share your hierarchy, I suspect there's something there that could be causing the problem.

Your problem lies in the GetComponent call I think. GetComponent only works when the thing you're getting is on the same game object as the script.

Instead of getting the canvas game object that way, make a public game object variable for the canvas and then drag and drop your canvas into that slot in the inspector.

Just like this:

public class YouWin : MonoBehaviour {
public GameObject canvas;

void OnTriggerEnter (Collider Collider)
{
canvas.SetActive(true);
}


All you have to do now is drag the canvas object into the slot that opens up in the inspector.

• It's still just not happening. I have exactly what you put. I also tried void OnTriggerEnter (Collider Collider) but that didn't work either. This script should be attached with the box collider right? – BondyeLwa Jul 30 '17 at 21:41
• The reason I changed it to (Collider Collider) is because unity was telling me I had to. – BondyeLwa Jul 30 '17 at 22:11
• Yeah I think Collider is correct actually, sorry about that, I just didn't even see that when I copy and pasted your code. Good practice is also to use camel case (lower case to start) for your argument name. Did you make sure to drag and drop the canvas game object into the slot in the inspector? – Superhq 2000 Jul 31 '17 at 15:25
• I put the canvas object into the slot in the inspector, yes. – BondyeLwa Jul 31 '17 at 16:39
• Works fine here . Make there is a rigid body. In play mode drag the player cube into the other cube. – Sidar Dec 25 '17 at 10:55