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?
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
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