Search Unity

Instantiated PreFabs all moving the same way but shouldn't

Discussion in 'Scripting' started by wizbit, May 16, 2017.

  1. wizbit

    wizbit

    Joined:
    May 10, 2017
    Posts:
    11
    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?
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Post your code
     
  3. wizbit

    wizbit

    Joined:
    May 10, 2017
    Posts:
    11
    This is from the RoomBuilder Class, which instantiates the Enemy Prefab. This has the Enemy Prefab added to the public GO slot in Unity.

    Code (CSharp):
    1. public GameObject enemy;
    2.  
    3. void PlaceEnemiesInRoom()
    4.     {
    5.         int x = 1;
    6.  
    7.         while (x <= noOfEnemies)
    8.         {
    9.             int xPos = Random.Range(0, xRoomDimension.Count);
    10.             int zPos = Random.Range(0, zRoomDimension.Count);
    11.  
    12.             enemyPlacement = new Vector3((float)xRoomDimension[xPos], 0F, (float)zRoomDimension[zPos]);
    13.  
    14.            
    15.                 var instance = Instantiate(enemy) as GameObject;
    16.  
    17.                 instance.transform.localPosition = enemyPlacement;
    18.  
    19.                 x++;
    20.            
    21.         }
    22.     }
    From the Enemy Class, attached to the Enemy prefab

    Code (CSharp):
    1. private void Update()
    2.     {
    3.         if (hasMoved)
    4.         {
    5.             hasMoved = false;
    6.  
    7.             StartCoroutine(EnemyPause());
    8.  
    9.             movement.enemyMove();
    10.         }
    11.     }
    12.  
    13.     IEnumerator EnemyPause()
    14.     {
    15.         yield return new WaitForSeconds(moveSpeed);
    16.         hasMoved = true;
    17.     }
    from the Movement Class, also attached to the Enemy Prefab

    Code (CSharp):
    1. public void enemyMove()
    2.     {
    3.  
    4.         enemyTurn();
    5.  
    6.     }
    7.  
    8.     public void enemyTurn()
    9.     {
    10.         System.Random rnd = new System.Random();
    11.         int caseTurn = rnd.Next(1, StartTurnLoop);
    12.  
    13.         switch (caseTurn)
    14.         {
    15.             case 1:
    16.                 //Right
    17.                 gameObject.transform.rotation = Quaternion.Euler(0F, 90F, 0F);
    18.                 break;
    19.  
    20.             case 2:
    21.                 //Left
    22.                 gameObject.transform.rotation = Quaternion.Euler(0F, -90F, 0F);
    23.                 break;
    24.  
    25.             case 3:
    26.                 //Forward
    27.                 gameObject.transform.rotation = Quaternion.Euler(0F, 0F, 0F);
    28.                 break;
    29.  
    30.             case 4:
    31.                 //Backward
    32.                 gameObject.transform.rotation = Quaternion.Euler(0F, 180F, 0F);
    33.                 break;
    34.  
    35.             default:
    36.                 gameObject.transform.rotation = Quaternion.Euler(0F, 0F, 0F);
    37.                 break;
    38.         }
    39.     }
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    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 :)
     
  5. wizbit

    wizbit

    Joined:
    May 10, 2017
    Posts:
    11
    i'll give it a try and update the post, thanks.
     
  6. wizbit

    wizbit

    Joined:
    May 10, 2017
    Posts:
    11
    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
     
  7. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    No problem :) Glad ya got it working. Enjoy your game making.
     
    wizbit likes this.