Instantiated PreFabs all moving the same way but shouldn't

Hey i have a an Enemy script which is attached to my Enemy Prefab. This script has a public damage & die methods. it also has private functions to move the enemy in a random fashion.

On its own this works well, and the enemies take damage and die and move as expected, but…

when i use my main play script to Instantiate the enemy prefab into my scene multiple times. ALL the cloned objects move in exactly the same random fashion!!!

if the random move is left turn, then they all left turn? Why is this happening?

Post your code

This is from the RoomBuilder Class, which instantiates the Enemy Prefab. This has the Enemy Prefab added to the public GO slot in Unity.

public GameObject enemy;

void PlaceEnemiesInRoom()
    {
        int x = 1;

        while (x <= noOfEnemies)
        {
            int xPos = Random.Range(0, xRoomDimension.Count);
            int zPos = Random.Range(0, zRoomDimension.Count);

            enemyPlacement = new Vector3((float)xRoomDimension[xPos], 0F, (float)zRoomDimension[zPos]);

           
                var instance = Instantiate(enemy) as GameObject;

                instance.transform.localPosition = enemyPlacement;

                x++;
           
        }
    }

From the Enemy Class, attached to the Enemy prefab

private void Update()
    {
        if (hasMoved)
        {
            hasMoved = false;

            StartCoroutine(EnemyPause());

            movement.enemyMove();
        }
    }

    IEnumerator EnemyPause()
    {
        yield return new WaitForSeconds(moveSpeed);
        hasMoved = true;
    }

from the Movement Class, also attached to the Enemy Prefab

public void enemyMove()
    {

        enemyTurn();

    }

    public void enemyTurn()
    {
        System.Random rnd = new System.Random();
        int caseTurn = rnd.Next(1, StartTurnLoop);

        switch (caseTurn)
        {
            case 1:
                //Right
                gameObject.transform.rotation = Quaternion.Euler(0F, 90F, 0F);
                break;

            case 2:
                //Left
                gameObject.transform.rotation = Quaternion.Euler(0F, -90F, 0F);
                break;

            case 3:
                //Forward
                gameObject.transform.rotation = Quaternion.Euler(0F, 0F, 0F);
                break;

            case 4:
                //Backward
                gameObject.transform.rotation = Quaternion.Euler(0F, 180F, 0F);
                break;

            default:
                gameObject.transform.rotation = Quaternion.Euler(0F, 0F, 0F);
                break;
        }
    }

You should either use Unity’s Random.Range or create a static random variable (and simply use ‘.Next()’ for each call you make).

This is quoted from the .Net docs:
" On most Windows systems, Random objects created within 15 milliseconds of one another are likely to have identical seed values. " The system clock has a finite resolution and calls made to it in succession will create the same values…
Hope that helps :slight_smile:

i’ll give it a try and update the post, thanks.

your fabulous. did the trick. changed the reference to Unity (Random.Range) and now the enemy object now moves independently…

i am new to Unity so am pickin gup a lot as i am going. this is perfect, thank-you

No problem :slight_smile: Glad ya got it working. Enjoy your game making.

1 Like