0
votes

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);
    }
}`
3

3 Answers

0
votes

You need to set the new position onto the object when you are done with calculating the next position. At the end of your enemyMove method, add this.

transform.position = new Vector3(newXPosition, newYPosition, transform.position.z);

Or use initialYPosition as 2nd parameter if you don't do calculations on the Y part.

0
votes

Wait, are you sure that initialXPosition = transform.position.x; is less value than 7? If not, place Debug.Log in

while (Mathf.Abs(newXPosition - initialXPosition) < 1)
{
    newXPosition = Random.Range(-6f, 6f);
    Debug.Log("Infinity");
}

Maybe you stuck in infinity loop? initialXPosition must be less value than 7 or it never exit this loop.

0
votes

This for those who might experience a similar issue:

What I needed to do was actually separate my movement and trigger functions from the master script into a script attached to the prefab.

Thank you to ray2yar on the unity answers forum for helping me sort this out!

Here's the code for the master script

void Start()
{
    SpawnEnemy = this;
    Spawn();
}

public void Spawn()
{

    GameObject enemyObject = Instantiate(enemyPreFab, enemyInitialPosition.position, transform.rotation) as GameObject;
    enemyObject.name = "Enemy";
    enemyObject.tag = "Enemy";
    enemyObject.transform.parent = transform;

}

And here's the code for the prefab's script

void Start()
{
    rigidbody2DComponent = GetComponent<Rigidbody2D>();
    initialYPosition = transform.position.y;
    Move();
}

void Move()
{
    speed = Random.Range(-10, -20);

    rigidbody2DComponent.velocity = new Vector2(0, speed);

    //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);
    }

    transform.position = new Vector3(newXPosition, initialYPosition, transform.position.z);
}

public void OnTriggerEnter2D(Collider2D other)
{
    if (other.tag == "Player" || other.tag == "resetWall")
    {

        //Lets let our master script take care of spawning
        Enemy.SpawnEnemy.Spawn();

        //Let's let our master script handle this
        //Without changing the name, the original name will get a bunch of
        //(clone) added to it as it respawns
        //newEnemyObject.name = "newEnemy";
        //newEnemyObject.tag = "Enemy";

        //Destroy the old enemy
        Destroy(gameObject);
    }
}