Answer the question
In order to leave comments, you need to log in
How to enable and disable Collider2D using the OnTriggerEnter2D/OnTriggerExit2D function?
Hello!
I want to make it so that when a player enters / exits the Trigger, Polygon Collider 2D turns on / off, I wrote a script, but error CS1002 pops up in lines 15 and 23, please tell me what this is connected with and how to fix it thanks in advance!
The Script itself:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TRUEFALSE : MonoBehaviour
{
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Player")
{
Collider 2D = gameObject.GetComponent<PolygonCollider2D>; /// CS1002
PolygonCollider2D = false;
}
}
void OnTriggerExit2D(Collider2D other)
{
if (other.tag == "Player")
{
Collider 2D = gameObject.GetComponent<PolygonCollider2D>; ///CS1002
PolygonCollider2D = true;
}
}
}
Answer the question
In order to leave comments, you need to log in
It's quite simple:
Consider this line
Collider 2D = gameObject.GetComponent<PolygonCollider2D>; /// CS1002
2D
does not matchPolygonCollider2D
is not convertible to type Collider
. Specify the same type of the returned component that you want to get by the GetComponent
. In this case, thisPolygonCollider2D
GetComponent
Collider 2D = gameObject.GetComponent<PolygonCollider2D>; ///CS1002
PolygonCollider2D = true;
other.enabled = true;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question