- Home /
How to activate all GameObjects named ,,GroundCollider'' by touching ,,GroundTriggerCollider'' and deactivating when no longer touching ''GroundCollider"?
The Object I refer to as ,,GroundCollider'' is a Physics Collider and ,,GroundTriggerCollider'' is a Trigger Collider.
Sorry. I overlooked Something while making the Visualization: In Panel 3, it's supposed to spell: ,,...and the Player doesn't fall through.''
Too unspecific. Do you mean when no longer touching the GroundTriggerCollider or GroundCollider? In general you would simply activating them when you get the OnTriggerEnter and you deactivate them when you get the OnTriggerExit callback. With which part do you have problems? Finding / referencing the colliders you want to activate / deactivate? Getting the callbacks to properly activating deactivating the objects? What have you tried so far? Show the current state of your code.
I added a Illustration to make the Question as Specific as I could. ( ͡° ͜ʖ ͡°)
Answer by ppiotrsz109 · Jun 19, 2018 at 03:55 PM
You have to create static bool in one class which will contain info about, does it have to activate. In separate script you add OnTriggerEnter and OnTriggerExit and in another case OnCollisionEnter and OnCollisionExit, and one bool to select if it's GroundTriggerCollider or GroundCollider.
Than just based static bool you just get all objects in scene using GameObject.FindObjectsOfType() and using System.Linq you use method find, where you select that x.name = "GroundCollider".
If you have any questions, write comment.
Sorry, but It's not specific enough.
How exactly is the Script written, that you mean?
Try like this: using UnityEngine; using System.Linq;
public class Collider$$anonymous$$anager : $$anonymous$$onoBehaviour {
public static bool GroundTriggerColliderTouching= false;
public static bool GroundColliderTouching = false;
GameObject[] colliders;
void Awake(){
colliders = GameObject.FindObjectsOfType<GameObject>().Where(x => x.name.Contains("GroundCollider"));
}
void FixedUpdate(){
if(GroundTriggerColliderTouching){
foreach(var item in colliders){
item.SetActive(true);
}
if(!GroundColliderTouching)
foreach(var item in colliders){
item.SetActive(false);
}
}
}
}
And now code for GroundTriggerCollider: using UnityEngine;
public class GroundTriggerCollider : $$anonymous$$onoBehaviour {
void OnTriggerEnter(Colider other){
Collider$$anonymous$$anager.GroundTriggerColliderTouching = true;
}
void OnTriggerExit(Colider other){
Collider$$anonymous$$anager.GroundTriggerColliderTouching = false;
}
}
And here the GroundCollider : using UnityEngine;
public class GroundCollider : $$anonymous$$onoBehaviour {
void OnCollisionEnter(Collision collision)
{
Collider$$anonymous$$anager.GroundColliderTouching = true;
}
void OnCollisionExit(Collision collision)
{
Collider$$anonymous$$anager.GroundColliderTouching = false;
}
}
Your answer
Follow this Question
Related Questions
Platform Effector 2D One Way, not working as expected 0 Answers
Comparing the value of a peg to its neighboring pegs' values 0 Answers
How do I make a bridge in game over an existing collision? 0 Answers
Trigger 2D (another gameObject) 4 Answers
Setting a Prefab Clone as the Child of another Object on Collision (2D) 1 Answer