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.