Hello,
While translating a script from Unityscript to C# I received compiler errors when trying to start the game. The source of errors came from the “CharacterDamage” script. This is the content of the script:
using UnityEngine;
using System.Collections;
public class CharacterDamage : MonoBehaviour
{
public Transform deadReplacement;
public AudioClip dieSound;
public float hitPoints = 100f;
public void ApplyDamage(float damage)
{
if (hitPoints > 0f)
{
hitPoints -= damage;
if (hitPoints <= 0f)
{
Detonate();
}
}
}
public static void CopyTransformsRecurse(Transform src, Transform dst)
{
dst.position = src.position;
dst.rotation = src.rotation;
foreach (Transform child in transform) {
Transform curSrc = src.Find(child.name);
if (curSrc)
CopyTransformsRecurse(curSrc, child);
}
}
public void Detonate()
{
Object.Destroy(gameObject);
if (dieSound != null)
{
AudioSource.PlayClipAtPoint(dieSound, transform.position);
}
if (deadReplacement != null)
{
Transform dst = Object.Instantiate(deadReplacement, transform.position, transform.rotation);
CopyTransformsRecurse(transform, dst);
}
}
}
The following errors I got are:
CharacterDamage.cs(27,45): error CS0120: An object reference is required to access non-static member `UnityEngine.Component.transform’
CharacterDamage.cs(43,23): error CS0266: Cannot implicitly convert type
UnityEngine.Object' to
UnityEngine.Transform’. An explicit conversion exists (are you missing a cast?)
Please, what are the reasons why I am getting the errors? And how do I solve the problem?
Thank you,
Xarlexus1