I have several instances of one prefab which have this PropertiesPrefab script attached:
public class PropertiesPrefab : MonoBehaviour {
public float health;
}
In other script, i want to access to the health of the PropertiesPrefab script of the prefab:
public class AnotherClass : MonoBehaviour
{
PropertiesPrefab propsPrefab;
void OnTriggerEnter(Collider other)
{
propsPrefab = other.GetComponent<PropertiesPrefab>();
}
void OnTriggerStay (Collider other)
{
// Wrong value because is not referenced to the component
Debug.Log(propsPrefab.health);
// Right value
Debug.Log(other.GetComponent<PropertiesPrefab>(). health);
}
}
The problem is that the debug.log sentences shows different values if the health has been changed. So the question is:
¿How can I get a component by reference, so when the health is changed, it also changes in the propsPrefab variable?
Your question is confusing. You do store a reference to the “PropertiesPrefabClass” script that has last entered the trigger. Are you sure there is only one object that is inside the trigger? You may want to use a proper Debug.Log statement like this:
Assets that can be used to find references have been released.
You can find and fix missing components or references.
Image:
Visual display of asset relationships.
Image:
Find detailed references to assets.
It’s a texture, but it’s also script.
It’s FREE! Please manage your assets conveniently and solve the problem of your project.