- Home /
Question by
legend0101 · May 19 at 11:04 AM ·
public variable
When I access a variable from different script it always return its initial value even if I change its value later in the first script?
public class Movement : MonoBehaviour {
[System.NonSerialized] public float _postion = 10f;
[SerializeField] ObjectMovement _object;
void Start()
{
_postion = 15f;
_object.spawnObject();
}
void OnTriggerEnter2D(Collider2D collision)
{
if(collision.gameObject.tag == "Trigger")
{
_postion = 20f;
_object.spawnObject();
Destroy(collision);
}
}
public class ObjectMovement : MonoBehaviour { [SerializeField] Movement _movement;
public void spawnObject()
{
Debug.Log(_movement._postion);
}
It still prints 10 (initial value) in console even if I changed the value later.
Please help me out
Comment
Your answer
Follow this Question
Related Questions
Assets/SplashScreen.cs(6,14): error CS1519: Unexpected symbol `public' in cl 1 Answer
is there a way to minimize amount of public items in inspector? 1 Answer
Assign a GameObject to a Component Public Variable using c# 1 Answer
Public and SerializedField's not showing in inspector 1 Answer
I have a little problem getting a component from another script 1 Answer