I'm very new to unity and I've been having a problem with my instantiated prefab. I'm trying to get my instantiated prefab to move once it loads into the scene, however the issue is that it doesn't move at all. The object loads into my scene, but it stays static. I've tried adding in Update() and FixedUpdate() and moving my enemyMove() into those, but it still doesn't work. I'm not sure of what the issue might be.
void Awake()
{
rigidbody2DComponent = enemyPrefab.GetComponent<Rigidbody2D>();
initialYPosition = transform.position.y;
}
void Start()
{
enemyObject = Instantiate(enemyPrefab, enemyInitialPosition.position, transform.rotation);
enemyObject.name = "Enemy";
enemyObject.transform.parent = transform;
enemyMove();
}
void enemyMove()
{
speed = Random.Range(-10, -20);
rigidbody2DComponent.AddForce(transform.up * speed, ForceMode2D.Force);
//keep track of the old x position
initialXPosition = transform.position.x;
//store the new x position
newXPosition = initialXPosition;
//new x position cannot be the same as the old x position
while (Mathf.Abs(newXPosition - initialXPosition) < 1)
{
newXPosition = Random.Range(-6f, 6f);
}
}
public void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Player" || other.tag == "resetWall")
{
enemyMove();
//instantiate a new enemy object everytime it hits player or the bottom wall
newEnemyObject = (GameObject) GameObject.Instantiate(enemyObject, new Vector2(newXPosition, initialYPosition), transform.rotation);
//Without changing the name, the original name will get a bunch of
//(clone) added to it as it respawns
newEnemyObject.name = "newEnemy";
//Destroy the old enemy
Destroy(this.gameObject);
}
}`