1
\$\begingroup\$

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?

\$\endgroup\$
  • \$\begingroup\$ Is this script on the object with the canvas? \$\endgroup\$ – Superhq 2000 Jul 30 '17 at 20:40
  • \$\begingroup\$ the script is attached to the box that i collide with \$\endgroup\$ – BondyeLwa Jul 30 '17 at 20:52
  • 1
    \$\begingroup\$ Why are you adding redundant brackets though? \$\endgroup\$ – Sidar Jul 31 '17 at 12:55
  • \$\begingroup\$ It is just leftover from trying other stuff. That isn't what's causing the problem though. \$\endgroup\$ – BondyeLwa Jul 31 '17 at 16:38
  • \$\begingroup\$ 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. \$\endgroup\$ – sjoerd216 Mar 6 '18 at 7:25
1
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ 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? \$\endgroup\$ – BondyeLwa Jul 30 '17 at 21:41
  • \$\begingroup\$ The reason I changed it to (Collider Collider) is because unity was telling me I had to. \$\endgroup\$ – BondyeLwa Jul 30 '17 at 22:11
  • \$\begingroup\$ 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? \$\endgroup\$ – Superhq 2000 Jul 31 '17 at 15:25
  • \$\begingroup\$ I put the canvas object into the slot in the inspector, yes. \$\endgroup\$ – BondyeLwa Jul 31 '17 at 16:39
  • 1
    \$\begingroup\$ Works fine here . Make there is a rigid body. In play mode drag the player cube into the other cube. \$\endgroup\$ – Sidar Dec 25 '17 at 10:55

Not the answer you're looking for? Browse other questions tagged or ask your own question.