Error Message from FPS Shooter Script

I’m doing quill18creates’ YouTube tutorial playlist on how to make a simple first person shooter. In the sixth video, he goes into making an enemy appear ‘dead’ by attempting to change the tags on an object using a C# script. I went through the tutorial and did the exact same things he did in the videos.

When I tried to run the game, I got a compiler error. Apparently, the error apparently has a lot to do with the tags being read-only (or so I assume).

Here’s the error message.

Assets/Scripts/BULLET_ThermalDetonator.cs(25,35): error CS0200: Property or indexer `UnityEngine.Collision.gameObject’ cannot be assigned to (it is read only)

Here’s the code:

using UnityEngine;
using System.Collections;

public class BULLET_ThermalDetonator : MonoBehaviour {

	float lifespan = 3.0f;
	public GameObject fireEffect;

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
		lifespan -= Time.deltaTime;

		if (lifespan <= 0) {
			Explode();
		}
	}

	void OnCollisionEnter(Collision collision) {
		if (collision.gameObject.tag == "Enemy") {
			collision.gameObject = "Untagged";
			Instantiate(fireEffect, collision.transform.position, Quaternion.identity);
			Destroy (gameObject);
		}
	}

	void Explode() {
		Destroy (gameObject);
	}
}

I’m hoping to know if anyone knows where I went wrong with the code. I’m still learning C#, so there are some things I have not yet learned.

At line 25, you are attempting to assign “Untagged” string to the game object (which you can’t).
You need to assign to the tag as so:

collision.gameObject.tag = "Untagged";

That’s where I went wrong!

Because I left out ‘tag’ on that single line, it wouldn’t compile. I added that extra little code on that line, and now it works.

I thank you for the help. It amazes me how even the simplest of mistakes in code cause a game to not compile. Sometimes I miss the little mistakes I do. :slight_smile: