Spawn Prefab in front of Prefab

Hi guys,

I was wondering if anyone could help me with this.

I spawn a cube on mouse click at the origin i.e. [0,0,0]. I want to be able to spawn another cube and so on at [2f,0,0] in the x axis while still keeping the old cube at [0,0,0].

    public GameObject Cube;
    List<Cube> ball = new List<Cube>();
    public float splitAngle = 30.0f;

    // Use this for initialization
    void Start ()
    {
        Instantiate(Cube, new Vector3(0, 0, 0), Quaternion.identity);
        spawnPrefab ();
    }

   
    // Update is called once per frame
    void Update ()
    {
        if (Input.GetMouseButtonDown (0))
        {
        for (int i = 0; i < 10; i++)
            {

            Instantiate(Cube, new Vector3(i * 1.0F, 0, 0), Quaternion.identity);

            }
        }
    }

    public void spawnPrefab()
    {

    }
}

I’ll really appreciate the help as I’m stumped right now.

Keep a “cursor”-like position variable in your class.

Vector3 cursorLocation;

In Start() set it to the initial value you want.

When you spawn a box, spawn it at that cursorLocation, then move the cursor location the way you want:

cursorLocation += new Vector3( 2.0f, 0, 0);

Thanks for replying. But I dont want it to spawn at a cursor location. I want it to spawn on an axis. I just want the mouse click to enable the spawn to happen.

“cursorLocation” is just the name of the variable. Set it to whatever you like, move it however you like, moving it once per spawn, if I understand your original question properly.

Ok Thank you. But I’m a little bit confused about where I add that second CursorLocation line.

At the function where you Instantiate the new prefab at the cursorLocation, then you advance the cursorLocation to the next place you want the next one to spawn.

That way the next time you call the function that does that, it will automatically appear at the next location, and so on.

Ok i got it working, thank you very much. I added the first cursor location to the Start() instead of declaring it as a Vector3.

You rule man. Thanks.