Trigger 2D not working

I’m working on a game that looks like an old submarine radar. There is the scanner that goes around the dial and when it hits an enemy, they become visible for a brief moment. Unfortunately, I can’t get the OnTriggerEnter2D to work when I attach this script to the enemy.

var fadeSpeed:float=5;

function Update(){

if(gameObject.renderer.material.color.a>0)
	gameObject.renderer.material.color.a-=fadeSpeed*Time.deltaTime;

}

function OnCollisionEnter2D(coll: Collision2D) {

if (coll.gameObject.tag == "Scanner"){
	gameObject.renderer.material.color.a=100;
	transform.position.x=100;
	}

}

When I have issues like this one, the first thing I do is put in Debug.Log() statements to determine where things are failing. It answers questions like:

  • Is OnCollisionEnter2D() being called?
  • If so what is the name and tag of the game object that is causing the call?
  • Am I getting to the code that is is to change the color?

I going to assume that your script is getting to the right code. If so, then your problem is here:

  gameObject.renderer.material.color.a=100;

The Color class in Unity has values between 0.0 and 1.0. So this line should be:

  renderer.material.color.a = 0.4;