- Home /
Need help with C# code. both gameObject and col.gameObject are getting destroyed. here is the code.
i have a few more if statements just like this one obviously changing the tags and player number each time. also is there a more efficient way of doing this. thanks
if (player[0] && (col.gameObject.tag == "2" || col.gameObject.tag == "3" || col.gameObject.tag == "4"))
{
Destroy (gameObject);
}
if (player[0] && col.gameObject.tag == "1")
{
Destroy(col.gameObject);
}
i want a square[0] to be destroyed by triangles(tag = 2) and circles(tag = 3) when the square comes into contact with them. also i want square[0] to destroy square(tag = 1) and not be destroyed by its self. the code bellow destroys both square[0] and any other object it comes in contact with. also if there is a more efficient way to put this please tell me. as on actually script i am dealing with 9 tagged objects and 9 array objects. thanks
What do you mean to check with if (player[0] && col.gameObject.tag == "1")
? Did you mean to check player's tag too. Now it jsut checks if player[0] is not null.
$$anonymous$$ight be possible to write it simpler but not with so little information. What is the general logic of how to destroy things? If you hit a shape that's the same as you, both get destroyed, otherwise just you?
if you(hero) hit same shape other shape(enemy) gets destroyed, if you hit different enemy, hero gets destroyed.
i have tagged the different shaped enemies differently. also the hero keeps changing shapes thats why heroes are in a player array
Answer by halfdevil333 · Aug 11, 2016 at 08:08 AM
50 lines of code cut down to this, thank you @NoseKills for placing the idea into my head.
void OnCollisionEnter2D (Collision2D col) {
if (gameObject.tag == col.gameObject.tag) {
Destroy(col.gameObject);
}
else { Destroy(gameObject); }
}
you dont know how happy i am. thanks man
Answer by NoseKills · Aug 10, 2016 at 08:10 PM
Assuming player[] array is an array of Unity Objects, right now your code checks
if (player[0] && (col.gameObject.tag == "2" || col.gameObject.tag == "3" || col.gameObject.tag == "4"))
if the object in player[0] is not destroyed/null, and the colliding object has tag 2, 3, or 4 => destroy this gameobject.
after that you do
if (player[0] && col.gameObject.tag == "1")
if the object in player[0] is not destroyed/null, and the colliding object has tag 1 => destroy the other gameobject.
There's no else
connecting the two if
s so you always do both checks.
if you(hero) hit same shape other shape(enemy) gets destroyed, if you hit different enemy, hero gets destroyed.
You can write this system more simply. Your are checking shapes so it would be a good starting point to make an object you can use to describe shapes. For example
public class Shape : MonoBehaviour {
public enum ShapeType {
Square,
Circle,
Triangle,
Pentagon,
}
public ShapeType type;
}
Attach that to all prefabs that need to have a shape and use the dropdown in the inspector to set the desired shape to all of them.
After that you can write the condition code almost like written text.
var playerShape = player[0].gameObject.GetComponent<Shape>();
var otherShape = col.gameObject.GetComponent<Shape>();
if (otherShape != null && playerShape != null) {
if (myShape == otherShape) { // if player and colliding shape are the same
Destroy(otherShape.gameObject);
} else {
Destroy(gameObject);
}
} else {
// this is just to check whether you remembered to attach Shape scripts
if (otherShape == null) {
Debug.LogError(col.gameObject.name + " did not have a 'Shape' Component");
} else {
Debug.LogError(player[0].gameObject.name + " did not have a 'Shape' Component");
}
}
Your answer
Follow this Question
Related Questions
Need a Custom array Index to look up gameobject tags? 2 Answers
Find and store gameObjects in a array 2 Answers
Hi, getting error cs1041 in my code:identifier expected, "this" is a keyword; need solution please 3 Answers
Unload from memory - GameObject in an array? 1 Answer
How to set to game objects's position from 2 different game objects arrays equal to each other? 0 Answers