Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

[Unity 2017 3.0f3] Instantiate Prefab does not change position

Discussion in 'Prefabs' started by ConfusedCactus, Mar 5, 2019.

  1. ConfusedCactus

    ConfusedCactus

    Joined:
    Jul 3, 2018
    Posts:
    19
    Hello Newbie here,

    I am trying to spawn some enemies with some distance from each other.
    I tried to change the position of a instantiated prefab, but nothing worked so far. I tried to change directly within this line "
    Code (CSharp):
    1.  GameObject enemy = Instantiate(enemyPrefab, enemyPrefab.transform.position + new Vector3(0, 15, 0) * i, Quaternion.identity);
    " But it does not work. I tried to change position after instantiating the object, but nothing happened either, as below
    Code (CSharp):
    1.  enemy.transform.position = enemyPrefab.transform.position + new Vector3(0, 15, 0) * i;
    .

    I think these are the two scripts that might causing the problem, one is attached to the enemy prefab to control its movement and where it is spawned initially (to left or right):
    Code (CSharp):
    1.  
    2. // initialize start space
    3.     private void Start()
    4.     {
    5.        
    6.         speed = Random.Range(minSpeed, maxSpeed); // random speed of worm
    7.         whereToSpawn(); // spawn left or right
    8.         endY = GameObject.Find("EndPos"); // where should the movement restart
    9.         size = Random.Range(minSize, maxSize); // randomize size of the worm
    10.         transform.localScale *= size; // change size
    11.         startY = transform.position.y; // y starting point
    12.         startX = transform.position.x; // x starting point
    13.     }
    14.  
    15.  
    16.     // Update is called once per frame
    17.     void FixedUpdate () {
    18.         transform.Translate(0, -speed * Time.deltaTime, 0); // move down
    19.  
    20.         // go back to start
    21.         if (transform.position.y <= endY.transform.position.y)
    22.         {
    23.             transform.position = new Vector3 (startX,startY + Random.Range(1,10), 0);
    24.          
    25.         }
    26.     }
    27.  
    28.  
    29.     public void whereToSpawn()
    30.     {
    31.        
    32.         leftSpawnPoint = GameObject.Find("LeftSpot");
    33.         rightSpawnPoint = GameObject.Find("RightSpot");
    34.  
    35.         int randomSpawnPoint = Random.Range(0, 2);
    36.  
    37.         // either spawn left or right
    38.         if (randomSpawnPoint == 1)
    39.         {
    40.             transform.position = rightSpawnPoint.transform.position;
    41.             transform.rotation = Quaternion.Euler(0, 180, 0); // if spawn right, need to rotate
    42.         }
    43.         else
    44.         {
    45.             transform.position = leftSpawnPoint.transform.position;
    46.         }
    47.  
    48.        
    49.     }
    And the other script SpawnEnemy, which is used to spawn a number of enemies:
    Code (CSharp):
    1.  
    2.     public GameObject enemyPrefab;
    3.     public int quantity;
    4.  
    5.     // Use this for initialization
    6.     void Start () {
    7.  
    8.         for (int i = 0; i < quantity; i++)
    9.         {
    10.             GameObject enemy = Instantiate(enemyPrefab);
    11.             enemy.name = "Worm" + i;
    12.             enemy.transform.position = enemyPrefab.transform.position + new Vector3(0, 15, 0) * i;
    13.          
    14.         }
    15.     }
    I checked on the forum and some saying this issue is a bug. I tried to fix it but nothing happened so far. Any help and advice welcome! Thanks in advance!
     
  2. ConfusedCactus

    ConfusedCactus

    Joined:
    Jul 3, 2018
    Posts:
    19
    Hi jrumps! Many thanks for your advice! Yes it is solved now! :) It works well on prefab with nothing attached. Turns out the prefab's start method is executed first, so what I do finally is to put the whereToSpawn() method and its relevant parts into the SpawnEnemy script. Many thanks again! :)
     
    runevision and jrumps like this.