This game is a pop-the-lock-style minigame. Every time I click in the point prefab the game loads another scene, but it is only supposed to do that when I click outside of the pointPrefab and after it goes past the pointPrefab without clicking.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class DestroyPoint : MonoBehaviour
{
bool triggered = false;
bool canClick = false;
Collider2D collidedObject;
private void Update()
{
if (!triggered && Input.GetKeyDown(KeyCode.Space))
{
Debug.Log("Game over");
SceneManager.LoadScene("FishingSpot");
}
else if (triggered && collidedObject != null && Input.GetKeyDown(KeyCode.Space))
{
canClick = false;
Debug.Log("Point Destroyed");
Destroy(collidedObject.gameObject);
collidedObject = null;
}
}
void OnTriggerEnter2D(Collider2D other)
{
Debug.Log("Entered");
if (other.gameObject.CompareTag("Point"))
{
canClick = true;
triggered = true;
collidedObject = other;
}
}
void OnTriggerExit2D(Collider2D other)
{
Debug.Log("exited");
if (canClick = true && other.gameObject.CompareTag("Point"))
{
collidedObject = null;
Debug.Log("Game Over exit");
SceneManager.LoadScene("FishingSpot");
}
if (other.gameObject.CompareTag("Point"))
{
canClick = false;
triggered = false;
collidedObject = null;
}
}
}
I was expecting to destroy the pointPrefab after clicking instead of the other scene being loaded.
if (canClick = true && other.gameObject.CompareTag("Point")) ...
One big issue I can observe is in your if
condition of OnTriggerExit2D()
method you're using assignment operator (=) instead of comparison (==). This will always set canClick
to true
and whenever you go outside of your trigger tagged Point
it will load FishingSpot
scene.
Instead change this to..
if (canClick && other.gameObject.CompareTag("Point"))
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments