- Home /
Changes to variables in script not being modified on objects attached to
I have a prefab projectile with a script on it that determines its movement. I had been modifying the values of the variable in a the provided text editor with the changes being applied to the prefab. This has stopped happening. Now I need to go to the prefab in the inspector and edit it there, reset the script from there. Why did this start happening and how can I fix it?
using UnityEngine;
using System.Collections;
public class tidy : MonoBehaviour {
public float CurveSpeed = 10;
//public float MoveSpeed = 1;
float fTime = 0;
Vector3 vLastPos = Vector3.zero;
// Use this for initialization
void Start () {
Destroy(gameObject,20);
}
public float speed= 5;
public int count=0;
// Update is called once per frame
void Update () {
vLastPos = transform.position;
fTime += Time.deltaTime * CurveSpeed;
Vector3 vSin = new Vector3(0, -Mathf.Sin(fTime), 0);
Vector3 vLin = transform.forward*speed;
transform.position += (vSin + vLin) * Time.deltaTime;
Debug.DrawLine(vLastPos, transform.position, Color.green, 100);
}
}
Answer by PAEvenson · Jul 11, 2013 at 06:02 PM
When you create a public variable in a script and assign it, you're assigning a default value to that variable. Once that gameObject gets serialized(attaching the script in the editor), the value gets serialized as well.
Changing the default value after that will have no effect on any object already serialized. It will, however, assign that default to newly instantiated scripts.
That does not explain why it was working before. The prefab isn't actually in the scene; copies of it are made when the fire button is pressed.
Your answer
![](https://koobas.hobune.stream/wayback/20220613105923im_/https://answers.unity.com/themes/thub/images/avi.jpg)