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 /
  • Help Room /
This question was closed Apr 05, 2021 at 07:19 PM by Tik115 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Tik115 · Apr 05, 2021 at 12:21 AM · triggerslistskeys

Have certain doors open when keys are used with an object

Hi, New-ish to programming here (re-learning after years of not doing any).

Currently I am working on a game as a learning experience and been using various tutorials online but I've hit a brick wall and unfortunatly I've not had luck in my searches for a solution.

The section of the game the player is in a room with 3 doors (Not including the one they came from) In the room is a cassette player which depending on the cassette that is in it will unlock a specific door. Currently I have a Key Script which allows me to assign a cassette colour to the object the script it attached to in the inspector

 [SerializeField] private CassetteType cassetteType; 
 public enum CassetteType
 {
     Red,
     Blue,
     Yellow,
 }
 public CassetteType GetCassetteType()
 {
     return cassetteType;
 }


The player has a script which holds the list of cassettes they have collected pulling from the above script. When they interact with the object that has a cassette colour assigned (Lets say Red) It stores the Cassette type in this list and destroys the game object so there can be no dupes, it also has a Debug displate to see if the Key script works which it does as when assigned to red it says "Added Red Key"

 public List<Key.CassetteType> cassetteList; 

 private void Awake() 
 {
     cassetteList = new List<Key.CassetteType>(); 

 }
 public void AddCassette(Key.CassetteType cassetteType)
 {   
     Debug.Log("Added " + cassetteType + " Key");


     cassetteList.Add(cassetteType);t
 }

 public bool ContainsCassette(Key.CassetteType cassetteType)
 {
     return cassetteList.Contains(cassetteType); 
 }

 private void OnTriggerStay(Collider other)
 {
     if (Input.GetKeyDown(KeyCode.E))
     {
         Key key = other.GetComponent<Key>();
         if (key != null)
         {
             AddCassette(key.GetCassetteType());
             Destroy(key.gameObject);
         }
     }
 }



Now here lies the issue, There is a cassette player in the room, when the player interacts with it they can choose what cassette they want to use. The thought process is that the object pulls the CassetteHolder script so when the players interact box triggers with it then they cassette players script will check the list to see what the player has triggering bool's which would allow the player to choose which of the 3 they want to put into the Cassette player, this then triggers a bool on the door script which will then lock or unlock the door.

Currently the Cassette player has the following code

 public GameObject Cassettecheck;
 
 bool redCasstte = false;
 bool blueCasstte = false;
 bool yelCassette = false;
 private void Start()
 {
     GetComponent<CassetteHolder>();
 }
 private void OnTriggerStay(Collider other)
 {
     //if (CassetteHolder.FindObjectOfType<Key.CassetteType.Red>())
     { redCasstte = true; }

 }



The commented part is where I've ran into the issue as it states Red doesn't exist in Cassette type which is true to begin with but won't be the case when the player picks up the red cassette. I tried seeing if Else Return did anything (Thinking it'll just skip it) but nope. I'm not sure if its the way I coding the other 2 scripts or just how I'm coding this one, or it could be all three scripts, I don't know.

Comment
Add comment · Show 2
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 KoenigX3 · Apr 05, 2021 at 08:54 AM 1
Share

If you want to check if the player has the cassette, why aren't you using ContainsCassette?

 CassetteHolder holderScript;
 
 void Start()
 {
    // You used GetComponent, but you did not do anything with it
    holderScript = GetComponent<CassetteHolder>();
 }
 
 void OnTriggerStay(Collider other)
 {
    if(holderScript.ContainsCassette(Key.CassetteType.Red)) redCasstte = true;
    // And so on...
 }


Also, it would be better to check only in OnTriggerEnter, since OnTriggerStay will be called too many times.

avatar image Tik115 KoenigX3 · Apr 05, 2021 at 07:18 PM 0
Share

This sorted out the issue, Guess I should of looked deeper into the bool formatting and calling other scripts (I've always had a hard time with those previously) I'll also impliment the OnTriggerEnter as opposed to OnTriggerStay, the practice stuff won't cause a performance drop or anything but its best not to take on any bad habbits.

Many thanks

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

158 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

Related Questions

List give a count of zero when it have 1 element 1 Answer

How to create a list in a specific order of one script that is used in different gameobjects? 0 Answers

Pipe Game ... I have to trace water from source to destination 2 Answers

When is OnTriggerExit2D called? 1 Answer

Can I know which exactly of an object's collider resulted in OnTriggerEnter()? 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