Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by miracleforcemanga · Sep 21, 2021 at 09:02 PM · scripting problemgameobjectstriggers

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);
         }
         
         
     }
 }
 
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image bdubbert · Sep 22, 2021 at 01:45 PM 0
Share

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?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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"

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

231 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Why wont my character stop moving when it collides with the obstacles?,Why doesn't my movement script stop? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges