2D box collider always detecting player,2D Box colliders detecting player when he is not there
So this code is supposed to disable the GameObject that is hiding another image underneath it when the player walks into its box collider (set to trigger). But when I start the game it doesn't show the GameObject even when I'm not colliding with it. My player has RigidBody2D and box collider 2D and some other scripts
,This is my code, I'm a beginner so that's probably the cause
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class SeeTroughWalls : MonoBehaviour { public GameObject currentshowable = null; public GameObject showable; bool seeTroughWalls = false;
private void Start()
{
showable.SetActive(true);
}
private void Update()
{
if(currentshowable = showable)
{
showable.SetActive(false);
}
else if(currentshowable = null)
{
showable.SetActive(true);
}
}
private void OnTriggerEnter2D(Collider2D other)
{
if(other.CompareTag("Player"))
{
currentshowable = showable;
}
}
private void OnTriggerExit2D(Collider2D other)
{
if (other.CompareTag("Player"))
{
currentshowable = null;
}
}
}
Basically I'm trying to disable a GameObject that is hiding another image behind it everytime a player walks inside the 2D box collider (set to trigger) that the GameObject has. My pkayer has "Player" tag, rigidbody2D and a box collider
Answer by robomarti · Oct 08, 2020 at 08:03 PM
FOUND AN ANSWER!
I just had to edit the code a bit, here's the finished code for anyone in future needing this: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class SeeTroughWalls : MonoBehaviour { public GameObject currentshowable = null; public GameObject showable; bool seeTroughWalls = false;
private void Start()
{
currentshowable = null;
showable.GetComponentInChildren<SpriteRenderer>().enabled = true;
}
private void OnTriggerEnter2D(Collider2D other)
{
if(other.CompareTag("Player"))
{
showable.GetComponentInChildren<SpriteRenderer>().enabled = false;
}
}
private void OnTriggerExit2D(Collider2D other)
{
if (other.CompareTag("Player"))
{
showable.GetComponentInChildren<SpriteRenderer>().enabled = true;
}
}
}
Your answer
Follow this Question
Related Questions
2D movement with gravity shifting 0 Answers
CS1216 error 0 Answers
"Invalid argument"? 0 Answers
Coyote Timer Gives Double Jump Frames. 1 Answer
How to make the next level load based on 2 characters touching a collider? 0 Answers