Damage for instantiated objects?

Hi,

I have the script below, used to damage player health. The script works fine if used on an object which is created in the editor, but when using the script on a prefab which is instantiated at runtime, I get the following error:

UnassignedReferenceException: The variable m_Transform of CharacterHealth has not been assigned.
You probably need to assign the m_Transform variable of the CharacterHealth script in the inspector.
UnityEngine.Transform.get_position () (at <86acb61e0d2b4b36bc20af11093be9a5>:0)
Opsive.UltimateCharacterController.Traits.Health.Damage (System.Single amount) (at Assets/ExternalPackages/Opsive/UltimateCharacterController/Scripts/Traits/Health.cs:206)
DealDamage.OnTriggerEnter (UnityEngine.Collider other) (at Assets/OpsiveScriptMods/Damage/DealDamage.cs:17)
The script is as follows:

using System.Collections; using System.Collections.Generic; using UnityEngine; using Opsive.UltimateCharacterController.Traits; public class DealDamage : MonoBehaviour { [Tooltip("The character that has the health component.")] [SerializeField] protected GameObject m_Character; public float m_DamageAmount = 10; public void OnTriggerEnter(Collider other) { if (other.CompareTag("Player")) { var health = m_Character.GetComponent<Health>(); health.Damage(m_DamageAmount); } } }

Does anyone know where to point me to get this solved? Am I approaching this incorrectly?
 
In the script above, the m_Character variable is probably never assigned to the current runtime character, as you instantiate it as a prefab. You have to assign it in the start method.
 
Top