Setting OnTriggerEnter to a second object collider
I like to keep these simplified instead of trying to explain everything that I'm doing, so here's the watered down version:
I have an object, called Object A
I have another object, called Object B
In its script, I tell Object A to do something if a bool is true
This bool only equals true if the player moves into the triggered collider of Object B
However, I don't really know how to do this. I was going to use OnTriggerEnter but with (ObjectB Collider other) instead of just (Collider other), though that doesn't seem to work. I'm working in C#. Anyone have any ideas?
Answer by itsharshdeep · Dec 01, 2015 at 03:34 PM
Hello
Sory I didn't understand the last part of the question. Can you please check the following link that whether is this you want?
Answer by ldeboer · Dec 01, 2015 at 08:55 PM
You have access to both objects in OnTriggerEnter just check the name
void OnTriggerEnter (Collider other) {
// Verify the collider is ObjectB by checking name
if (other.name == "Whatever Object B is named") {
// set your bool or whatever you want to do
yourBoolName = true;
}
}
Assuming you want set the bool false on exit you use OnTriggerExit.
void OnTriggerExit (Collider other) {
// Verify the collider is ObjectB by checking name
if (other.name == "Whatever Object B is named") {
// Clear your bool or whatever you want to do
yourBoolName = false;
}
}
Thanks for the swift response!
In this case, the collider I'm grabbing in other.name is actually the player object's. I want my script to detect when the player has contacted Object B.
Okay so Object B is an object with the trigger collider and this script. Your player need the bool as a public or write a public function that changes the bool.
So ObjectB will call to ObjectA (player) when triggered ... I will do the public bool method
So this script is on the static objectB and you have a collider and script with a bool on the player. I will call it "PlayerScript" in this code you need to change that to your classname of your script.
void OnTriggerEnter (Collider other) {
// Verify the collider is ObjectA (A$$anonymous$$A the player) by checking name
// You could optionally check the tag if you have multiple players
if (other.name == "Whatever player is named") {
// Since we know this is our player Fetch the script off the player
// This will fail and return null if it wasn't a player or script not on
PlayerScript temp = other.GetComponent<PlayerScript>();
// set the player script bool directly ... it must be a public
if (temp != null) temp.yourBoolName = true;
}
}
Your answer
Follow this Question
Related Questions
Delete object with key within Trigger 1 Answer
Detect Volume on Collision 1 Answer
C# OnTriggerEnter Issue 3 Answers
How Do I make a 3D Numbered Padlock? 0 Answers
RAW image won't show on UI 0 Answers