- Home /
How To Stop Destroyer From Destroying?
Hey I have a question, how can I stop the destroyer(it's a big quad that destroys all leftover gameobjects) from destroying certain game objects? You see i have power ups in the game that give me extra points, and every time the destroyer destroys them I get points for it, which of course i don't want to happen. So how can stop the destroyer from destroying the power ups? Or how can I stop the power up from adding scores when destroyed by the destroyer? Here's my destroyer script and power up script so you know what I'm talking about:
public class DestroyerScript : MonoBehaviour {
HUDScript hud;
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Player")
{
Application.LoadLevel(1);
return;
}
if (other.gameObject.transform.parent)
{
Destroy (other.gameObject.transform.parent.gameObject);
}
else
{
Destroy (other.gameObject);
}
}
}
Powerup:
public class Powerup : MonoBehaviour {
ScoringScript hud;
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Player")
Destroy (gameObject);
{
hud = GameObject.Find ("Character").GetComponent<ScoringScript> ();
hud.IncreaseScore (3);
}
}
}
And the script that calculates my score:
public class ScoringScript : MonoBehaviour {
float playerScore = 0;
bool isPaused = false;
public bool scoring = true;
public float guiPlacementY1;
public float guiPlacementX1;
PlayerScript player;
void Start()
{
player = GetComponent<PlayerScript >();
scoring = true;
}
public void IncreaseScore(int amount)
{
playerScore += amount;
}
public void DecreaseScore(int amount)
{
playerScore -= amount;
}
void OnDisable()
{
PlayerPrefs.SetInt ("Score", (int)(playerScore * 5));
PlayerPrefs.SetString("PlayerName", "High Score");
PlayerPrefs.Save ();
Debug.Log("Hide");
}
void OnGUI()
{
GUI.Label (new Rect(Screen.width * guiPlacementX1, Screen.height * guiPlacementY1, Screen.width * .5f, Screen.height * .4f), "Score: " + (int)(playerScore * 5));
}
}
Answer by AgeTDev · Aug 27, 2014 at 07:19 AM
If I understand you correctly, you dont want your destroyer to destroy the powerups when they collide, right ? So , I see you already use a tag to check whether the Player is colliding with the destroyer or something else, so what you could do is just use another if-condition to see if the "other" object is actually a powerup.
Lets say you put a tag on them called powerup,then you could add this line :
if (other.tag != "powerup")
{
//Your existing code here
/*
if (other.gameObject.transform.parent)
{
Destroy(other.gameObject.transform.parent.gameObject);
}
else
{
Destroy (other.gameObject);
}
*/
}
Also , i would add an else-statement after the if that checks if your Player is hit.
Hope i can help, cheers, T
Edit : and ofcourse what the Person before me suggested , namely the Destroy call in your powerup Needs to be inside the brackets.
Let me make sure i got this right. So like this?:
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Powerup")
{
if (other.gameObject.transform.parent)
{
Destroy (other.gameObject.transform.parent.gameObject);
}
else
{
Destroy (other.gameObject);
}
}
else
if(other.tag == "Player")
{
Application.LoadLevel(1);
return;
}
}
I'd say the other way around, first check if it IS the Player , and then check if IT IS NOT the power up , because if it is the power up , you do not want to do anything with it , right ?
so with your code :
void OnTriggerEnter2D(Collider2D other)
{
if(other.tag == "Player")
{
Application.LoadLevel(1);
return;
}
else
{
if (other.tag != "Powerup")
{
if (other.gameObject.transform.parent)
{
Destroy (other.gameObject.transform.parent.gameObject);
}
else
{
Destroy (other.gameObject);
}
}
}
}
The important part is the " != powerup " because then it will only perform your Actions if the tag is something else. and because you checked if it is the Player before, this shoudl be what you were asking for :)
hope it works, T
Sadly it's not working. Isn't there just a way to tell the destroyer script to completely ignore the power up? I'm creating a short level, so it shouldn't affect the performance by that much.
Thats what this should Do, Are you Sure you did add the tag to the PowerPCs ?
Yep, I added the tag to the power up prefab and the game object/spawn. It must be something else that's causing this to not work. I'll have to look into it more.
Answer by JustFun · Aug 27, 2014 at 06:35 AM
You have a mistake in powerup script:
if (other.tag == "Player") Destroy (gameObject);
{
hud = GameObject.Find ("Character").GetComponent<ScoringScript> ();
hud.IncreaseScore (3);
}
With such placed braces, IncreaseScore() is called always, regardless of colliding objects. You need to move Destroy() into braces:
if (other.tag == "Player")
{
hud = GameObject.Find ("Character").GetComponent<ScoringScript> ();
hud.IncreaseScore (3);
Destroy (gameObject);
}
I see it appears I missed that. I just changed it, it didn't affect anything. The power ups still increase my score when they get destroyed by the destroyer. Thanks though for trying to help me. I appreciate it. :)
Is powerup script the only place, where IncreaseScore() function is called?
No. I have other scripts but those are disabled. Other than that, there is no other place where IncreaseScore is called.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers
Why does this do nothing (Health Script) 2 Answers
Hit Collision with exceptions 2 Answers
How to Destroy a gameobject on collision 3 Answers