The question is answered, right answer was accepted
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.
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.
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
Follow this Question
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