Search Unity

Prefab doesn't instantiate at object

Discussion in 'Scripting' started by CodeWurm, Nov 20, 2019.

  1. CodeWurm

    CodeWurm

    Joined:
    Nov 8, 2014
    Posts:
    316
    Why does my prefab instantiate in the middle of the screen while the spawn script is set on an empty object and the empty object is outside the scene, where I want to clouds to be spawned.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections;
    3. using UnityEngine;
    4.  
    5. namespace Script
    6. {
    7.     public class CloudSpawn : MonoBehaviour
    8.     {
    9.         public GameObject clouds;
    10.         public int spawnTimer = 3;
    11.  
    12.         public float maxHeight = 10;
    13.         public float minHeight = 1;
    14.         public float height;
    15.  
    16.         // Start is called before the first frame update
    17.         public void Start()
    18.         {
    19.             height = Random.Range(minHeight, maxHeight);
    20.             StartCoroutine(CloudsWave());
    21.         }
    22.  
    23.         private IEnumerator CloudsWave()
    24.         {
    25.             while (true)
    26.             {
    27.                 Instantiate(clouds,  transform.position, transform.rotation, Random.Range(height));
    28.                 yield return new WaitForSeconds(spawnTimer);
    29.             }
    30.         }
    31.     }
    32. }
    33. }
     
    Last edited: Nov 20, 2019
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    If you don't give Instantiate() a position, then it defaults to (0,0,0). Bear in mind, the code of the Instantiate function has no idea what object is calling Instantiate(), so it can't default to that position.

    You can use Instantiate(clouds, transform.position, transform.rotation); to make it instantiate at the current object's position.
     
    Joe-Censored likes this.
  3. CodeWurm

    CodeWurm

    Joined:
    Nov 8, 2014
    Posts:
    316
    How would I add the random height to it, like this?
    Code (CSharp):
    1. Instantiate(clouds,  transform.position, transform.rotation, Random.Range(height));
     
  4. koirat

    koirat

    Joined:
    Jul 7, 2012
    Posts:
    2,073
    You sure about this, I have always thought that it will instantiate with the transform values as are set in the prefab.
     
  5. koirat

    koirat

    Joined:
    Jul 7, 2012
    Posts:
    2,073
    How about
    Code (CSharp):
    1. Instantiate(clouds,  transform.position+Vector3.up*Random.value*height, transform.rotation);
     
    CodeWurm likes this.
  6. CodeWurm

    CodeWurm

    Joined:
    Nov 8, 2014
    Posts:
    316
    Ai that works well!!! , thanks.
     
  7. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Ah, good point, that is correct.
     
  8. CodeWurm

    CodeWurm

    Joined:
    Nov 8, 2014
    Posts:
    316
    Why at this line:
    Code (CSharp):
    1. Instantiate(clouds, transform.position + randomHeight * Random.value * Vector3.up, transform.rotation);
    I get an underscore at transform.rotation.
    saying, Repeated property access of built in component is inefficient.
     
  9. koirat

    koirat

    Joined:
    Jul 7, 2012
    Posts:
    2,073
    That is surprising I never get it.
    You may try to store it:
    Quaternion rotation = transform.rotation;
    and then use "rotation" variable.