- Home /
Create tag in script, then check for it in collision
I've followed the Tutorial Project: Space Shooter, and now want to modify it so only certain asteroids give points. I'm trying to do this by giving the asteroids a tag, through scripting, after instantiating. Unfortunately the asteroids in the collider check with the lasers ("DestroyByContact") the asteroids continue to have the tag "Untagged". I've tried asteroid.tag, asteroid.collider.tag, asteroid.rigidbody.tag, ...
From: GameController.cs
public IEnumerator SpawnWaves() {
// ...
while (true) {
for (int i = 0; i < hazardCount; i++) {
// ...
GameObject asteroid = Instantiate (hazard, spawnPosition, spawnRotation) as GameObject;
if (Random.value > 0.75f) {
asteroid.tag = "MetalAsteroid";
//Debug.Log("Spawned 1 point");
}
yield return new WaitForSeconds (spawnWait);
}
// etc...
}
}
From: DestroyOnContact.cs
void OnTriggerEnter(Collider other) {
int scoreValue = 0;
if (other.tag == "Boundary") { // OK
return;
}
if (other.tag == "Player") { // OK
Instantiate (playerExplosion, other.transform.position, other.transform.rotation);
gameController.GameOver ();
}
if (other.tag == "MetalAsteroid") { // Never gets called
Debug.Log ("Shot 1 point");
scoreValue = 1;
}
Debug.Log("Tag: " + other.tag); // Untagged when expecting MetalAsteroid
Instantiate (explosion, transform.position, transform.rotation);
gameController.AddScore (scoreValue);
Destroy (other.gameObject);
Destroy (gameObject);
}
Answer by fafase · Feb 08, 2014 at 03:49 PM
I think you cannot give tag other than the one in the list. So you cannot just create one at runtime, you need to place it first in the list.
Correct, but not doing so creates a run-time error. There are no run-time errors or warnings in the console log.
I don't get what you mean. if you have the tag registered then you can assign it. Else you won't. Create a new tag and assign in script.
The issue wasn't that I couldn't assign it, rather that it seemed to have disappeared during the check. Thanks to your insistence that the code was indeed correct, I found the actual logical error through a process of eli$$anonymous$$ation. Specifically, the collision script is attached to the asteroids, not the laser bolts. Thus 'other' is the laser bolt, not the asteroid, and ins$$anonymous$$d of other.tag I should be using gameObject.tag.
Correct code for completion's sake:
void OnTriggerEnter(Collider other) {
int scoreValue = 0;
if (other.tag == "Boundary") {
return;
}
if (other.tag == "Player") {
Instantiate (playerExplosion, other.transform.position, other.transform.rotation);
gameController.GameOver ();
}
if (gameObject.tag == "$$anonymous$$etalAsteroid") {
Debug.Log ("Shot 1 point");
scoreValue = 1;
}
Debug.Log("Tag: " + other.tag);
Instantiate (explosion, transform.position, transform.rotation);
gameController.AddScore (scoreValue);
Destroy (other.gameObject);
Destroy (gameObject);
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613132913im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
OnCollision Script... 2 Answers
Gun swap through collision 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Substitute of collision 2 Answers
Ignore collision on tag not working 2 Answers