- Home /
Why isn't my instance of an object moving?
Hey, I'm pretty new to unity and am making an infinite runner at the moment. I made it so that the platforms under the character move instead of the actual character.However, when I clone the platform, it doesn't move, even though I have movement set up in it's update function. The first 2 place holder platforms I added in do move, so I am really stumped.
Here is the platform spawner script: public class EnviroHandler : MonoBehaviour {
public GameObject ground;
private float groundWidth;
private float interval;
private float intervalProgression = 0;
private ArrayList groundList = new ArrayList();
// Use this for initialization
void Start () {
groundWidth = ground.GetComponent<BoxCollider2D>().size.x;
interval = ground.GetComponent<groundMovement>().speed;
}
// Update is called once per frame
void Update () {
if(intervalProgression >= interval){
groundList.Add(Instantiate(ground, new Vector3(9.2f, -2, 0), Quaternion.identity));
intervalProgression = 0;
}else{
intervalProgression += Time.deltaTime;
}
}
}
and here is the script each platform comes with:
public class groundMovement : MonoBehaviour {
//declaration for certain attributes
public int speed = 3;
public float endPos;
public float width;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
// Move the object to the left, using speed vasriable
transform.Translate(Vector3.left * Time.deltaTime * speed);
//destroy object when it moves off screen, based on x position and width
if (transform.position.x >= endPos + width){
Debug.Log("destroyed");
Destroy(this);
}
}
}
Any help is greatly appreciated.
Answer by Strixie13 · Jul 13, 2018 at 02:17 PM
Check the object after it is instantiated, is the public int speed
variable still set to three in the inspector on the clone? Sometimes it will show up as 3 when you create the object manually, but revert to 0 when it is instantiate if you declare it like that. (Thus it would be multiplying by 0 and would not move) If that is the case either do public static int speed = 3
or void Awake(){speed = 3;}
to make sure it is declared.
This is because the property values are copied from the prefab. The script does instantiate a prefab. If the prefab has speed set at 0, it will copy that value. Check the value of the prefab.
Yeah I just checked and the clones don't even have the script. How can I fix that?
You have to instantiate a prefab with the script rather than an object already in the scene. Get the object all set up how you want it, then drag and drop it into a folder. After that delete the object in the scene. Now go to your manager script, drag and drop the object from the folder onto the public GameObject ground
.
Alternatively, if you already have a prefab (object in a folder) and made changes in the scene, click "apply" at the top right hand of the object's inspector window to apply the changes.
Your answer
![](https://koobas.hobune.stream/wayback/20220612170634im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
2D jump with Raycast 1 Answer
Instantiate object at position, moving in a direction? 1 Answer
Ground dash is like a teleport 0 Answers
AI Movement Direction 0 Answers