Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How to set where a prefab Instantiate's? [C#]

Discussion in 'Scripting' started by EthanWZech, Jul 28, 2017.

  1. EthanWZech

    EthanWZech

    Joined:
    Feb 25, 2017
    Posts:
    27
    So I'm new to Unity and programming in general but for the life of me I can't find out how to set where my prefab spawns. Here's my code.


    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class QueenCreate : MonoBehaviour {

    public GameObject worker;

    // Use this for initialization
    void Start () {

    Instantiate(worker);

    }

    // Update is called once per frame
    void Update () {

    }
    }
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,133
  3. TSC

    TSC

    Joined:
    Oct 15, 2012
    Posts:
    271
    Code (CSharp):
    1.  
    2. public Vector3 wotsText;
    3. public GameObject wots;
    4. public GameObject SP;
    5.  
    6. public void SomeMethod()
    7. {
    8. GameObject WotS;
    9. WotS = (GameObject)Instantiate(wots, SP.transform.position, SP.transform.rotation);
    10. WotS.transform.SetParent(SP.transform);  
    11. wotsText=wots.transform.position;
    12. }
     
  4. EthanWZech

    EthanWZech

    Joined:
    Feb 25, 2017
    Posts:
    27
    Yeah I knew I was missing something with the code stuff. Honestly I didn't look very hard. Anyway's I looked at the ScriptReference but I didn't really understand it.

    Thanks for that I've finally got it sorted out!