- Home /
How do I deactivate the drag and drop after the Panel is dropped in its right place?
So, Im making a game in which the player will drag and drop cards in their respective places. When he doesnt put the card in the right place, it goes back to its initial position. I put a script for when the player drops the card at the right place, he earns 10 points. When it reaches 100, it goes to the next scene. The problem is, the player can just keep putting the same card into the same place, and the counter keeps going. How do i block the card after it is in its right place?
Im using Event Trigger for the drag, drop and the score. The object being dragged is a Panel.
Manager
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Manager : MonoBehaviour
{
public GameObject titulo, publico, tituloPlace, publicoPlace;
Vector2 tituloInitialPos, publicoInitialPos;
public AudioSource source;
public AudioClip correct;
bool tituloCorrect, publicoCorrect = false;
void Start()
{
tituloInitialPos = titulo.transform.position;
publicoInitialPos = publico.transform.position;
}
public void DragTitulo()
{ titulo.transform.position = Input.mousePosition; }
public void DragPublico()
{ publico.transform.position = Input.mousePosition; }
//---------------------------------------------------------
public void DropTitulo()
{
float Distance = Vector3.Distance(titulo.transform.position, tituloPlace.transform.position);
if (Distance < 50)
{
titulo.transform.position = tituloPlace.transform.position;
source.clip = correct;
source.Play();
tituloCorrect = true;
}
else
{
titulo.transform.position = tituloInitialPos;
source.clip = correct;
source.Play();
}
}
public void DropPublico()
{
float Distance = Vector3.Distance(publico.transform.position, publicoPlace.transform.position);
if (Distance < 50)
{
publico.transform.position = publicoPlace.transform.position;
source.clip = correct;
source.Play();
publicoCorrect = true;
}
else
{
publico.transform.position = publicoInitialPos;
source.clip = correct;
source.Play();
}
}
public void Pontos()
{
if (tituloCorrect)
{
ScoreScript.scoreValue += 10;
tituloCorrect = false;
}
if (publicoCorrect)
{
ScoreScript.scoreValue += 10;
publicoCorrect = false;
} }}
Score script (if needed)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class ScoreScript : MonoBehaviour
{
private int nextSceneToLoad;
public static int scoreValue = 0;
Text score;
void Start()
{
score = GetComponent<Text>();
nextSceneToLoad = SceneManager.GetActiveScene().buildIndex + 1;
}
public void selectScene()
{
switch (this.gameObject.name)
{
case "NextScene":
SceneManager.LoadScene(nextSceneToLoad);
break;
}}
// Update is called once per frame
void Update()
{
score.text = "" + scoreValue;
if (scoreValue >= 100)
{
SceneManager.LoadScene("Scene02");
}}}
Im new at C# and unity, so i hope im being clear.
Answer by Zymurer · Feb 13, 2021 at 09:49 PM
Why you cannot use if clause?
public void DragTitulo()
{
if(tituloCorrect == false){
``titulo.transform.position = Input.mousePosition;
}
What happens when you use it?
Im new at c#, I'm not sure how to use it. I tried that, but it just doesn't move
public void DragTitulo() { if (tituloCorrect == false) {titulo.transform.position = Input.mousePosition; } }`
Your answer
Follow this Question
Related Questions
How to make reference from prefab to variable via C#/JavaScript? 1 Answer
can't drag and drop to the scene 1 Answer
How to Simplify My Drag and Drop Inventory 1 Answer
Modular building system. 0 Answers
Camera dragging problem in 2D 0 Answers