- Home /
make collider enabled when another object is destroyed
hey guys , how do i make a sphere collider enabled when another object tagged as "TNT" is destroyed , i have this variables but i dunno how to group them into a script , im quit noob at coding , can anyone help me please ? thanks ! (the sphere collider is in an empty object)
if (other.CompareTag("TNT")){ Destroy(other.gameObject); collider.enabled = false;
So the TNT gets destroyed, I guess you have that part, and you want a sphere collider to get enabled, is that collider attached to the TNT object? Is there a lot of this in your game? If you plan on having many TNT objects, are they all connected to one collider of their own or do they need to figure out the closest one or else?
Finally, what are you doing with the collider? because you might be going to wrong way, for instance if your collider is meant to be used for the explosion (just mentionning). That is quite a few questions but they should help us help you (now I sound like a school counselor).
hey @fafase thanks for your help i really appreciate it , now what i want of ALL of this , my concept is to create like a chain reaction (explosions),i have TNT(1) and TNT(2) (near TNT(1)) , there's an emptyobject that have two childs which are the TNT(1) and an emptyobject that has a spherecollider, i want when TNT(1) gets exploded(destroyed) a spherecollider of an emptyobject will be enabled, , i want the spherecollider to be enabled when TNT(1) gets destroyed , and when the spherecollider will be enabled , the TNT(2) will trigger the spherecollider , and TNT(2) will be destroyed too , that's all , hope you will understand my concept , thanks !
ok if you have those two TNT only (and even if you have more), you can do it another way. Now considering TNT1 will always trigger TNT regadless the distance, you don't need the sphere:
public class TNTScript:$$anonymous$$onoBehaviour{
public GameObject otherTNT;
void Explosion(){
// Here particle system and destroy
if(otherTNT != null)
otherTNT.GetComponent<TNTScript>().Explosion();
}
}
Place this script in both TNTs. Drag TNT2 into the slot in the inspector of TNT1 and this is it.
Now if there should be a check for distance and find any TNT within range (some ind of Angry Birds system), then you better have an array or list of TNT boxes, then when one is exploding, check if there are nay within range and make them explode as well. They will also look for TNT in range and will in turn call for explosion and so on.
@fafase thanks for the reply dude , yeah my point is the "RANGE" , i want to use the spherecallider as a "RANGE" to destroy another TNT which is in that range , not all the TNT's that are in the scene .
Answer by DoctorMoney · Feb 25, 2014 at 03:40 PM
We might need more code to understand exactly what you want and how you're going about it but for starters:
collider.enabled = false;
should be
collider.enabled = true;
if it's false then you're disabling the collider
DUUUUUUUUH dude this is obvious i already know that false is for disabling the collider and true for enabling , i just mentioned that in my question to let people know that im doing a progress not just sitting there and waiting for an answer
Answer by fafase · Feb 25, 2014 at 04:15 PM
public class TNTScript:MonoBehaviour{
static List<TNTScript> list = new List<TNTScript>();
public bool exploding = false;
void Start(){
list.Add(this);
}
void Explosion(){
for (int i = 0; i < list.Count; i++){
float dist = (transform.position - list[i].transform.position).sqrMagnitude;
exploding = true;
if(dist < range && list[i].exploding == false){
list[i].GetComponent<TNTScript>().Explosion(gameObject);
}
}
list.Remove(gameObject);
Destroy(gameObject);
}
}
Ok, so you need to try that as I cannot. The idea is that the list gathers all of the TNT boxes, you could do it in a way that you avoid static for the sake of time, I will do it this way.
The method iterates through the list and checks for any object that would be close enough. If so, it calls the Explosion on the other box.
To avoid TNT boxes to call it each other endlessly the exploding boolean is set and check.
Your answer
Follow this Question
Related Questions
Internal collisions 1 Answer
Something wrong with destroying script 0 Answers
Making a bullet invinceble 0 Answers
How did I screw up BoxColliders? 1 Answer
col.gameObject.layer is not working 1 Answer