- Home /
How to Remove script from an prefab object on collision?
hi i am trying to remove Deathpart script from instantiated prefab platform when its collide with an object. this is the code i have written to add DeathPart Script randomly to child parts of platform, its working fine . Now i wanted to remove this DeathPart script from child part whenever its collide with an object (tag name "PowerUp")
public void LoadLevel()
{
for (int i = 0; i < platforms.Count; i++)
{
spawnPosZ += platformDistance;
GameObject platform = Instantiate(PlatformParts, transform);
platform.transform.localPosition = new Vector3(0, 0, spawnPosZ);
List<GameObject> childParts = new List<GameObject>();
foreach (Transform t in platform.transform)
{
t.GetComponent<Renderer>().material.color = levelPlatformColor;
childParts.Add(t.gameObject);
}
List<GameObject> deathparts = new List<GameObject>();
while (deathparts.Count < platforms[i].deathPartCount)
{
GameObject randomPart = childParts[(UnityEngine.Random.Range(0, childParts.Count))];
if (!deathparts.Contains(randomPart))
{
randomPart.gameObject.AddComponent<DeathPart>();
deathparts.Add(randomPart);
}
}
}
here is code i have written to remove DeathPart from child parts and i attached this script to each child part but its not working
public class RemoveDeathPart : MonoBehaviour
{
if (collision.gameObject.tag == "PowerUp")
{
Destroy(GetComponent<DeathPart>());
}
$$anonymous$$ake sure that the tag is correctly set for all the objects, essentially make sure you're colliding with an object you're trying to collide with. Also, make sure that the collider matches the collision detection method you're using.
Also probably worth explaining what you mean by "not working".
Is it throwing an exception? (in which case try checking for existence of the component before destroying it)
Or is it just not removing the component? If so have you actually looked at the object in the hierarchy or logged its components, to check that it's set up as you think it is.
Also I note that you're not checking for non-existence of the component before you add it. Is it possible that you're ending up with multiple components? In which case it might be possible that you are removing one, but it's not changing the behaviour.
Thank you
"not working"-i mean nothing happening
i checked the hierarchy its not removing the component
If the object has a single DeathPart component, and that line is being called, then the component will be destroyed.
So if it's not being destroyed, the line's not being called. And that means that either the function isn't being called or the object doesn't have the desired Tag.
So you want to find out what i happening and the way to do that is to add a load of logging.
Starting with logging in the function with the important code in, so that you can find out exactly when the line is being called and when it isn't.
I'd do something along these lines...
public class RemoveDeathPart : $$anonymous$$onoBehaviour
{
private void $$anonymous$$yFunction()
{
if (collision.gameObject.tag == "PowerUp")
{
Debug.LogFormat("$$anonymous$$yFunction called on {0} with PowerUp Tag", gameObject.name);
DeathPart dp = GetComponent<DeathPart>();
if (dp !=null)
{
Destroy(dp);
}
else
{
Debug.LogErrorFormat("{0} has no DeathPart!", gameObject.name);
}
}
else
{
Debug.LogFormat("$$anonymous$$yFunction called on {0} without PowerUp Tag", gameObject.name);
}
}
}
I notice you don't actually show us this function with the important code in it. So I've called it $$anonymous$$yFunction so as not to make any assumptions. If it's a collision handler of some sort and the function's not being called at all then maybe you given it the wrong name (or the objects aren't set up right with colliders/triggers, or the physics set-up is wrong, and so on).
$$anonymous$$ake sure you don't have "Collapse" turned on in the Console (you should almost never use that when debugging).
I am not sure that I can understand your setup completely, but I have three things on my $$anonymous$$d. First, make sure the objects have a collider, otherwise it won't work for sure. Second, if you need two scripts to interact with each other, you have to link them together them, if that's not the case, ignore this suggestion. Third, I can see that you used "Death*Part" and "Deathp*art", so make sure your names match.
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Script not recognizing collision 3 Answers
Multiple Cars not working 1 Answer
Particle System Play Works Inconsistently 0 Answers
Destroy temp object and make new object. 0 Answers