Search Unity

How to spawn an prefab

Discussion in 'Scripting' started by Pizzarules668, Feb 14, 2020.

  1. Pizzarules668

    Pizzarules668

    Joined:
    Jan 31, 2020
    Posts:
    4
    I want to spawn a prefab at 0, 18, 0 when the script starts. I have this and it spawns at 0, 0, 0
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Spawner : MonoBehaviour
    6. {
    7.     public GameObject Prefab1;
    8.     void Start(){
    9.         Instantiate(Prefab1, new Vector3(0f,10f,0f), Quaternion.identity);
    10.         transform.Translate(0, 18, 0);
    11.     }
    12. }
     
  2. Vectorbox

    Vectorbox

    Joined:
    Jan 27, 2014
    Posts:
    232
    The following code (line 10) is setting the position of the Spawner script parent object as opposed to the instantiated prefab

    Code (csharp):
    1. transform.Translate(0, 18, 0);
    Try this...

    Code (csharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Spawner : MonoBehaviour
    6. {
    7.     public GameObject Prefab1;
    8.     //
    9.     void Start(){
    10.         Instantiate(Prefab1, new Vector3(0f,18f,0f), Quaternion.identity);
    11.     }
    12. }
     
    Last edited: Feb 15, 2020
  3. Pizzarules668

    Pizzarules668

    Joined:
    Jan 31, 2020
    Posts:
    4
    It still spawns at 0, 0, 0
     
  4. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    Does your Prefab contain a script that handles its own position and potentially sets it to (0,0,0)? The code Vectorbox showed should work for spawning it at 18f height, so if it does not end up there, something else changed the position.
     
    Vectorbox likes this.
  5. Vectorbox

    Vectorbox

    Joined:
    Jan 27, 2014
    Posts:
    232
    As Yoreki says, the revised script I posted does return the required result with a 'standard' prefab. If you can't identify the issue, post a download link to your prefab and I'll take a look.

    Unity Manual
    Instantiate Object
     
    Last edited: Feb 16, 2020
  6. ciorbyn

    ciorbyn

    Joined:
    Oct 17, 2013
    Posts:
    138
    The transform put by itself refers to the spawner object.
    While you want to change the ownership of the spawned object then:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class Spawner : MonoBehaviour
    4. {
    5.     public GameObject Prefab1;
    6.     void Start()
    7.     {
    8.         GameObject go = Instantiate(Prefab1, new Vector3(0f, 10f, 0f), Quaternion.identity);
    9.         go.transform.Translate(0, 8, 0);
    10.     }
    11. }
     
    Pizzarules668 likes this.
  7. Pizzarules668

    Pizzarules668

    Joined:
    Jan 31, 2020
    Posts:
    4
    Thank you ciorbyn it works now
     
  8. ciorbyn

    ciorbyn

    Joined:
    Oct 17, 2013
    Posts:
    138
    You are welcome! ;)
     
  9. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    What ciorbyn (and also Vectorbox in his first reply) said about transform.Translate moving the current object is of course true, but.. based on what OP wrote, that's not what he / she asked for. OP asked how to spawn something at 18f height. Where something spawns is handled with the second parameter of Instantiate(). Why would we spawn it at 10 and move it up 8, when we can spawn it at 18? Am i missing something?
     
    Vectorbox likes this.
  10. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    Yup, @unity_bnFYkXS0r3Iu4w is still at the crowbar-coding stage, getting their bearings: "It doesn't matter how you get there - as long as you do get there" :)

    That being said, you'd be surprised how many seasoned programmers unthinkingly spawn-then-position rather than spawn-at-position. I dimly seem to even remember some Unity-backed tutorials that do this.
     
  11. ciorbyn

    ciorbyn

    Joined:
    Oct 17, 2013
    Posts:
    138
    Yes, the correct code is:

    Code (CSharp):
    1.     void Start(){
    2.         Instantiate(Prefab1, new Vector3(0f,18f,0f), Quaternion.identity);
    3.     }
    But as it didn't work and the rest is unknown, I limited myself to literally correcting the original code.
    The fact that the instance at (0,18,0) didn't work for me also remains a mystery. o_O
     
    Last edited: Feb 17, 2020
    orionsyndrome likes this.
  12. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,105
    lol, nice placebo
     
  13. Pizzarules668

    Pizzarules668

    Joined:
    Jan 31, 2020
    Posts:
    4
    Thank you all for helping, this is the first time I have worked in unity that is not a tutorials.
     
    Vectorbox likes this.