- Home /
how do I use the same script for 2 different purposes?
hi! I am developing a top-down puzzle game, and I am having trouble with door locking/unlocking. essentially, I wanted to have a pressure plate for the first level, and I wanted to add a key, and "break" the pressure plate. I tried doing it in its own script, but it wouldn't work no matter what I tried. so I resorted to using the pressure plate script, but the thing is, I have it so an object with the tag named "Block" unlocks the door when an item with the pressure plate script attached is under it/touching it as a trigger. this is a problem because The key is under one of the blocks. I tried creating an isDoorLocked boolean and used its values with the logic, nothing. just keeps the door locked. I have it so now it has to collide with an object with the tag "Player" to unlock the door, but it now keeps the door unlocked, which is not what I want.
here is both the level change script(the one for the door) and the pressurePlateFunctionality script(obviously it's for the pressure plate/key.)
PressurePlate.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PressurePlate : MonoBehaviour
{
// detect when an object is staying on top of the trigger
static public bool pressurePlateHeld = false;
void OnTriggerStay2D(Collider2D collider){
if(collider.gameObject.CompareTag("Player")){
Destroy(gameObject, 0);
Debug.Log("Key Collected");
}
else{
pressurePlateHeld = true;
Debug.Log("Presser plate held down.");
}
}
}
LevelChange.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class LevelChange : MonoBehaviour
{
public string levelName;
void Start(){
PressurePlate.pressurePlateHeld = false;
}
public void OnCollisionEnter2D(Collision2D collision){
if(PressurePlate.pressurePlateHeld == false){
Debug.Log("Door is locked.");
}
else{
PressurePlate.pressurePlateHeld = true;
Debug.Log("Scene Changed");
SceneManager.LoadScene(levelName);
}
}
}
Wording on your question is a bit confusing, but you never set pressurePlateHeld back to false so your door will definitely stay unlocked. You would have to set it to false in OnCollisionExit to re-lock the door. Is this the problem you were trying to solve?
Answer by Chimz · Sep 25, 2021 at 08:31 PM
I want to help but I can't quite understand the situation. Can you please explain it a little simpler? Or even better, provide some images or a Screen Video with you explaining what's going on? We (me and other members of community) are not in your shoes so we have no idea what your project is like. And so reading your text is a bit complicated and can't quite understand what's going on, like what this below means
"add a key, and "break" the pressure"
Your answer
Follow this Question
Related Questions
Why does the money counter resets when I pull different objects trough the trigger? 1 Answer
[C#] Activating set amount of objects to spawn another object? 0 Answers
How to generate objects from a script in the editor, i.e. before runtime? 1 Answer
How to get raycast only detect first object from the same masked layer.. 0 Answers