- Home /
C# collider problem with tags/GameObject.names
Hi all, I'm trying to make a very basic game, just a quick run down, I have 4 targets which zoom across the screen at randomized momments, a blue sphere, red capsual, yellow square, ect. Now each bullet is a different color. The goal is to hit the targets that have the same color as your bullets.
Now with my code, I am able to fire a bullet, straight line and it will know to destroy itself and any targets it will hit. the issue I'm having is that in terms of the targets (not the background it recognizes this) it's not grabbing the correct tag or game object name (I keep changing it around to look for .tag and .name, still getting the same issue.
So in a nut shell this code isn't recognizing the correct tag of the targets and always printing out the phrase "Miss" I have checked, when I use tags the tag is always spelled exactly the same, and I have attached the right game objects to the different GameObjects in unity.:
using UnityEngine;
using System.Collections;
public class Col_Det : MonoBehaviour {
public GameObject self;
public GameObject targetTag;
public string scored;
// Use this for initialization
void Start () {
scored = "NoHit";
}
// Update is called once per frame
void Update () {
if(scored != "NoHit"){
print (scored);
scored = "NoHit";
}
}
void OnTriggerEnter(Collider other) {
if(other.gameObject.tag == "BlueTraget"){
scored = "Score";
print(scored);
}
else if (other.gameObject.tag != "BlueTarget"){
scored = "Miss";
print(scored);
}
if(other.gameObject.tag != "Background"){
Destroy(other.gameObject);
}
Destroy (self);
}
}
Spelling error: "BlueTraget" ins$$anonymous$$d of "BlueTarget". See my answer below for additional advice.
Answer by syclamoth · May 13, 2012 at 05:35 AM
EDIT: nah, it's a spelling error. Disregard my previous post. What you are seeing here is the basic problem with using string literals for things- it'll run through the compiler just fine, but as soon as you put it in game it won't work for reasons that are difficult to track down. Basically, you are spelling 'BlueTarget' wrong in one of those if-statements, and that's breaking everything.
I think you should set that up in a clearer fashion. Instead of using this 'hit tags, check strings' thing, you can just do it all automatically using collision layers! Go to the 'layers and tags' settings window and set up unique layers for each of your object types. Then, make sure all your projectiles and targets have the correct layers in their prefabs. Once you've done that, clever manipulation of the layer-based collision detection matrix in the physics settings will allow you to have only certain object collide with other objects. Unless you don't want the things to pass right through each other? In which case you can use a public LayerMask in your bullet prefab, and use the bitwise operations to work out whether it should collide or not. If not, you may choose to delete the object.
Your answer
Follow this Question
Related Questions
Collider Confusion 1 Answer
I'm trying to adjust tagged objects meshrenderer in C# code. 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
C# Check if Collider2D[] Doesn't contain Scripted GameObject 1 Answer