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);
}