Why does my game load another scene when i click? UNITY 2D

CreatoMan

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.

Sujoy Ghosh
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.

edited at
0

Comments

0 comments
Login to comment

Related

Unity 2d game - why is my static stat resetting to 100 when scene changes?

Unity first game project - How do I change scene when reaching my goal area?

Trying to load a new scene when I enter a cube in Unity

Why does my button's style change when I click on it?

Why does my js modal not close when I click outside?

Why does my form not appear when I click the button?

Why is the game scene empty when I press "play"?

Why my GPU load is low when is rendering a scene with Blender?

2d unity game asking permissions which i didn't mentioned in my code

Why does exporting to Unity WebGL change the speed of objects in my game?

Why is my Java game crashing when I add a game loop?

Unity 2D When Zoom In Scene Disappears

Scene disappears when zooming in Unity 2D

My function does work when I click the button (Angular 2)

Why does my game character keep accelerating when I use the keydown event?

Why does my Vertex Animation shader produce different results in Scene and Game view?

Why does the value of the random bound reset when I change the scene?

Why does bluetooth disconnect when I move to a new scene?

Why doesnt my scene finish loading Unity?

Why does my if statement work if I manually change the checked state, but not when I click the button

friction not working correctly in my 2d game - unity

I want to create in-house store for my 2D game in unity should I create it by using PlayerPrefs or Serializedfield

Unity 2D Why does my character jump different heights every time?

Why does my 2D movement in Unity only move up and left but not down and right?

Why the character in 2d game falling when running the game ?

why does my LAMP stack show my image when I navigate to it w/ a URL but not when I load it w/jQuery and PHP

I'm having an error in unity for a script that changes the scene to another scene

How do I clear my game screen and move to a new scene when "play" is clicked

Why does my while loop break at end even when I make sure my game_start variable is still 0?